Interesting question, but nonetheless no, the magnitude of the int
has no significance as per Comparable<T>
and Comparator<T>
specifications, only the sign. Conceivably some sorting algorithm can additionally specify that they can take "hints" from the magnitude, but I'm not sure how practical that would be for comparison-based sorting, since we really need only to know if a < b
, a == b
, or a > b
(which is really what Comparable
and Comparator
are OOP abstractions of).
Now it needs to be said that there may be a hidden intention here of using the subtraction idiom for comparing numeric values, i.e. something like this:
public int compare(T t1, T t2) {
return t1.intField - t2.intField;
}
Do note that this comparison method is potentially broken, due to possible overflow when the difference between the two numbers is greater than Integer.MAX_VALUE
. In fact, this is one of the puzzles covered in Java Puzzlers.
To demonstrate, consider the following snippet (taken from the book):
int x = -2000000000;
int z = 2000000000;
System.out.println(x - z); // prints a positive number due to overflow
Clearly x < z
, and yet x - z
is a positive number. Beware of using this subtraction idiom: it's always much safer to do an explicit comparison and return -1
, 0
, or 1
instead.