Sort by Multiple Fields
Tie-break with tuple keys and reverse.
Sort by Multiple Fields is a free Coding Interview Prep lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Coding Interview Prep learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
When One Key Is Not Enough
Sometimes items tie on the first field and you must break the tie. The fix is a tuple key that lists fields in priority order.
Tuples Compare Left to Right
Python compares tuples element by element: it checks the first item, and only looks at the second when the first ties.
print((1, 9) < (1, 2)) # False
print((1, 2) < (2, 0)) # TrueA Tuple Key for Two Fields
Return a tuple from your key to sort by the first field, then the second. This sorts people by age, then by name.
people.sort(key=lambda p: (p.age, p.name))Primary and Secondary Order
The leftmost field is the primary sort and the next is the tie-breaker. Reorder the tuple to change which field rules.
data = [(2, "b"), (1, "z"), (1, "a")]
print(sorted(data))
# [(1, 'a'), (1, 'z'), (2, 'b')]Mix Ascending and Descending
To sort one field up and another down, negate the numeric field you want descending inside the tuple key.
items.sort(key=lambda x: (x.group, -x.score))Negation Trick Needs Numbers
The minus-sign trick only works on numbers. To reverse a string field you must layer sorts instead of negating.
Stable Sort Lets You Layer
Because sorting is stable, you can sort by the minor key first, then by the major key, and ties stay correctly ordered.
rows.sort(key=lambda r: r.name)
rows.sort(key=lambda r: r.age, reverse=True)Layering vs One Tuple
One tuple key is faster and clearer for simple cases. Layered sorts shine when one field goes up as a string while another goes down.
itemgetter for Speed
For index-based tuples, itemgetter is a tidy, fast key. It plucks several fields at once in priority order.
from operator import itemgetter
rows.sort(key=itemgetter(1, 0))Tie-Breaks Make Output Deterministic
Adding a final unique tie-breaker like an id makes output stable across runs, which judges with strict checkers appreciate.
Plan Your Key Before Coding
Read the problem and write down the sort fields in order. A clear tuple key turns a messy comparison into one short line.
Quick Check
You want students sorted by grade ascending, then by name ascending.
Recap
You can break ties with a tuple key, negate numbers for descending, and layer stable sorts for mixed directions. Tie-breaks keep output clean. ✅
Frequently asked questions
Is the “Sort by Multiple Fields” lesson free?
Yes — the full text of “Sort by Multiple Fields” is free to read here on the web, and the Coding Interview Prep course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Coding Interview Prep course, upgrade to CoddyKit PRO.
What will I learn in “Sort by Multiple Fields”?
Tie-break with tuple keys and reverse. You practise Coding Interview Prep with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Coding Interview Prep?
No prior experience is required. Coding Interview Prep on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Sort by Multiple Fields” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Coding Interview Prep lesson?
Yes. Every Coding Interview Prep lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.