Using the key Parameter for Custom Sorting
The sort function compares tuples element-by-element, starting with the first position and moving to subsequent positions only when a tie occurs.
The First Decision
When items are represented as tuples, their order is decided from left to right. The sort function begins with the first element of each tuple. It considers later elements only when the earlier elements are tied. This gives tuple sorting a predictable, lexicographic ordering.
What do you think happens?
Which tuple comes first when comparing ('Alice', 90) and ('Bob', 85)?
Reveal answer
Answer: ('Alice', 90)
The first elements are different, so the comparison is decided at the first position. The score is not consulted because there is no tie in the names.
Reading Tuple Positions
Think of each tuple position as a sorting criterion with a priority. The first position has the highest priority because it is checked first. The second position matters only if the first positions match. The third position matters only if both the first and second positions match. This process continues through the tuple until a difference determines the order.
A Difference at the First Position
Compare ('Alice', 90, 2) with ('Bob', 85, 1).
Check the first elements: The first tuple begins with Alice and the second begins with Bob.
Stop at the first difference: Because the first elements differ, the comparison is decided at that position. The scores and years do not resolve this comparison.
The tuple beginning with Alice comes before the tuple beginning with Bob.
Resolving Ties
A tie at one position does not end the comparison. Instead, sorting advances to the next position. For student records written as name, score, and year, names are compared first. If two names match, scores are compared. If both names and scores match, years are compared. The order of the tuple fields therefore determines which criterion receives priority.
Two Equal Names
Compare ('Alice', 90, 2) with ('Alice', 85, 1).
Compare the names: Both tuples have Alice in the first position, so the first comparison is tied.
Move to the scores: The scores are 90 and 85. The comparison is decided at the second position.
Ignore the year for this pair: Because the scores differ, the third position is not needed to determine the order.
The tuple with score 85 is ordered before the tuple with score 90 when the comparison is increasing.
Increasing and Decreasing Order
The reverse=True keyword argument reverses the entire comparison logic. With tuple records, this means the ordering is decreasing at every level: later names come first, and within a matching name group, higher scores come first. The reversal applies to the complete tuple comparison rather than only to one selected position.
| Sorting direction | First position | Tie-breaking positions |
|---|---|---|
| Increasing | Earlier values come first | Later positions are compared in increasing order |
| Decreasing with reverse=True | Later values come first | Later positions are compared in decreasing order |
The source describes student records and gives a decreasing-order result: ('Charlie', 85), ('Bob', 90), ('Alice', 90), ('Alice', 85). Charlie comes before Bob and Alice because the names are later in the alphabet. Within the Alice group, the score 90 comes before 85 because the comparison is decreasing.
Predicting the Final Order
To predict a sorted sequence, inspect pairs of tuples from left to right. First group your attention around the first element, because it has the highest priority. Within a group sharing that first element, compare the second element. Continue to the third element only when the earlier positions remain tied. This method follows the same decision process used by tuple comparison.
Predict the decreasing-order arrangement of these records: ('Alice', 85), ('Charlie', 85), ('Bob', 90), ('Alice', 90). Explain which position decides each important comparison.
Hints
- Compare the names before comparing the scores.
- The two Alice records are tied at the first position.
- Because the order is decreasing, compare the Alice scores from higher to lower.
Common Sorting Mistakes
Assuming the second element always controls the order
The first elements differ, so the comparison is decided before the scores are considered.
Fix:
Always begin with the first tuple position and move right only after a tie.Stopping when the first elements tie
A tie in one position means the next position must be checked.
Fix:
Continue to the second element, then to later elements if necessary.Reversing only the first criterion
The source describes reverse=True as reversing the entire comparison logic.
Fix:
Apply decreasing-order reasoning to every tuple position.Putting the most important criterion later
Earlier positions receive priority during comparison.
Fix:
Place the most important criterion in the first position.
Key Takeaways
- Tuple sorting checks elements from the first position toward later positions.
- A later element is used only when all earlier compared elements are tied.
- The first tuple position represents the highest-priority sorting criterion.
- reverse=True reverses the ordering at every tuple position.
- Predict the result by tracing the first position first, then following tie-breaks to the right.
Key Takeaways
- Sorting tuples uses a left-to-right, element-by-element comparison.
- Ties are resolved by comparing the next tuple element.
- Tuple design determines which sorting criteria receive priority.
- reverse=True produces decreasing order throughout the comparison.
- A reliable prediction strategy is to identify the first differing position for each pair.