I find it very satisfying that sorting methods in the JDK can detect if your comparer is not obeying the contract.
There are a lot of errors that cause the contract to be violated according to .
Here are the rules in short as described in :
- sign(compare(x,y)) == -sign(compare(y,x))
- (compare(x, y)>0) && (compare(y, z)>0)) implies compare(x, z)>0 (transitivity)
- compare(x, y)==0 implies that sgn(compare(x, z))==sgn(compare(y, z))
-  GitHub - Flyway - Issue 1249
-  StackOverflow - “Comparison method violates its general contract!”
-  Oracle Javadoc - Interface Comparator>T>