Sorting a tuple based on one of the fields
Codemia Knowledge Hub ·
Introduction
Sorting tuples by one field in Python is usually a sorted(..., key=...) problem. The core idea is simple: tell Python which tuple position matters for ordering, and let the built-in sort handle the rest.
Core Sections
Sort by one tuple position
Suppose each tuple is (name, age), and you want to sort by age. The normal solution is a key function that returns the second element.
Output:
The tuples themselves are not changed. sorted returns a new list ordered by the value returned from the key function.
Use itemgetter for clarity
For tuple-like data, operator.itemgetter often reads more cleanly than a lambda.
This does the same thing as lambda person: person[1]. It is mostly a readability choice, but it is common Python style for simple tuple sorting.
Ascending versus descending order
Sorting defaults to ascending order. To reverse it, add reverse=True.
That is better than negating numeric values manually because it keeps the intent obvious.
Sort by multiple fields
If the target field can tie, return a tuple key. Python compares tuple keys left to right.
Output:
Here the primary sort is age and the secondary sort is name.
Sorting a tuple of tuples
If the outer container is itself a tuple, sorted still returns a list because sorting creates a new ordered sequence.
If you really need a tuple back, convert afterward:
That distinction matters because the question is often phrased as "sort a tuple," but Python's sort operation returns a list unless you convert it.
sorted() versus .sort()
Use sorted() when:
- the input might not already be a list
- you want to preserve the original container
- you prefer an expression that returns a new object
Use .sort() when you already have a list and want to reorder it in place.
That avoids allocating another list, but only works on mutable lists.
Common Pitfalls
- Forgetting that
sorted()returns a new list rather than modifying the original data. - Using the wrong tuple index and sorting by the wrong field.
- Assuming the result keeps tuple type when the input container is a tuple;
sorted()still returns a list. - Writing a complex lambda when
itemgetteror a tuple key would be clearer. - Ignoring ties and then being surprised when equal primary values are not ordered the way you expected.
Summary
- Use
sorted(data, key=lambda row: row[index])to sort tuples by one field. - '
itemgetter(index)is a clean alternative for simple tuple indexing.' - Add
reverse=Truefor descending order. - Return a tuple key when you need secondary sort behavior.
- Remember that
sorted()always returns a list, even if the original container was a tuple.
Related reading
- Sorting algorithm of Arrays in Java.util package
- Sorting algorithm to implement highest total combinations
- Sorting algorithm to keep equal values separated
- sorting algorithm where pairwise-comparison can return more information than -1, 0, 1
- Sorting an Array in TensorFlow
- Sorting arrays in NumPy by column
- Sorting algorithms for data of known statistical distribution?
- Sorting an almost sorted array elements misplaced by no more than k
System design interview problems on this topic
- Design a Web Crawler: solution and walkthrough
- Design a Task Scheduler: solution and walkthrough
- Design a URL Shortener (TinyURL): solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.