Implement the ORDER BY of a small reporting tool. Write sort_records(records, keys):
- •
records is a list of dicts. - •
keys is a list of (field, direction) pairs, most important first; direction is "asc" or "desc".
Rules:
- •A missing field counts as
None, and None sorts last for that key in both directions (like NULLS LAST). - •Within one field, the non-
None values are all the same kind (all numbers, or all strings), so they can be compared with each other — but strings can be sorted descending too. - •Records that are equal on every key keep their input order.
Return a new sorted list; don't change records.
Python 3.13 in your browser — the standard library plus pandas and numpy; no pip installs.