A comparator invariant is a consistency property—such as transitivity—that comparison results must preserve across values. Subtracting integers in compareTo can violate it through overflow, producing incorrect ordering. section 7.11
The subtraction trap
With values 2,000,000,000, 0, and −2,000,000,000, `return i - o.i` can report contradictory ordering because the subtraction overflows. Replace it with `Integer.compare(i, o.i)` or an explicit three-way comparison.
Prevent comparator-invariant failures by:
- Test large values, larger collections, and sorted collections such as TreeSet; violations can corrupt collection behavior, not merely sorting order. section 7.11
