Card 09/ 09

RecapDifficulty: Advanced1 min

One Natural Order, Many Requested Ones

Five things this topic established, and where each one was.

What the topic established, and which card it was on
The thingWhere
Every sort rests on one two-argument question: which of these comes firstTeaching Java the Order Your Type Already Has
Comparable puts the order on the type; Comparator puts it at the callComparable or Comparator: Whose Order Is It
thenComparing only runs on a tie, and reversed() reverses everything to its leftBuilding a Comparator That Sorts on Two Fields
Subtracting two int values can overflow and flip the signWhy Subtracting Two ints in a Comparator Is a Bug
An inconsistent comparator throws on some lists and silently misorders othersWhy Your Sort Threw

The one worth carrying: build comparators out of comparing and thenComparing rather than writing the arithmetic. Both failures in this topic come from hand-written comparison logic, and both disappear when the combinators write it for you.