Assuming that a comparator or compareTo
method always returns -1 or 1 if the first operand is less than or greater than the second is
incorrect.
The specifications for both methods, Comparator.compare
and Comparable.compareTo
, state that their return value is "a
negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object." Even if a specific
comparator always returns -1, 0, or 1, this is only an implementation detail, not the API contract developers can rely on.