Skip to content

Sorting

Read · 1 of 3

sorted with a key

sorted(nums) gives a new sorted list (smallest first). Add reverse=True for biggest first.

To sort by your own rule, give a key: a function that turns each item into what to compare. key=lambda s: (-s.marks, s.name) sorts by marks high to low, then by name.