AlgoFrames

Sort by a bounded key, stably

Challenge · Write code · about 20 min

Each record is a string "grade:name", such as "90:eli", with a grade from 0 to 100. Return the records sorted by grade, ascending. Records with the same grade must keep their input order.

Use counting sort on the grade.

Example 1

Input:

[["3:ann","1:bob","3:cy","2:dee"]]

Output:

["1:bob","2:dee","3:ann","3:cy"]

ann stays ahead of cy.

Example 2

Input:

[[]]

Output:

[]

Constraints

  • 0 ≤ records.length ≤ 1,000
  • Each record is "grade:name" with 0 ≤ grade ≤ 100
  • Equal grades keep their input order

Learn the idea first: Counting sort, a visual lesson.

More Counting sort problems