I saw this post about reversing order with lambdas, but one thing that's confused me even with traditional Comparator implementations (or anonymous inner classes) is: why does comparing the second argument with the first argument reverse the order?
For example:
public int compare(Integer o1, Integer o2) {
return o2.compareTo(o1);
}
In the above snippet, integers will be sorted in reverse order. However, o1.compareTo(o2)
results in a normal order. Why does switching the order of comparison work, and why does this extend to other custom comparators?