Sorting
Python's sorted() uses Timsort (O(n log n)). Stable, fast, built-in.
Code example
# sort by score descending leaderboard = sorted(rows, key=lambda r: r.score, reverse=True) # multi-key sort: score desc, then time asc leaderboard = sorted(rows, key=lambda r: (-r.score, r.elapsed_ms)) # in-place rows.sort(key=lambda r: r.score, reverse=True)
How it works
Sorting arranges items in order. Python's sorted() returns a new list; list.sort() sorts in place. Both use Timsort (a hybrid merge/insertion sort) at O(n log n). The key= argument lets you sort by a computed field — sorted() calls key on each item and sorts by the result. The leaderboard code uses sorted(rows, key=lambda r: -r.score) to rank by score descending. The negative-sign trick turns a descending sort into an ascending sort on the negated key.
🔍 Where this is used in OpenBenchML
app/routes/leaderboard.py — sorted() with key= to rank by score / speed / size.