Skip to content

Sorting · routine

Sort by a key

scores is a list of [name, points] pairs. Write ranking(scores) returning the names from most points to fewest; equal points keep their original order.

Here sorted is allowed: pass it key= (what to sort by) and reverse=True. Python's sort is stable — equal keys keep their order.

Run adds print(ranking([["ada", 3], ["bob", 5], ["cy", 3]])) after your code, to try it.

Stuck on the idea rather than the code? The Merge sort, and sorting by a key lesson walks through it.