In Java, Integer.compare(x, y) compares two int values. It returns a negative number when x is less than y, zero when they are equal, and a positive number when x is greater. In practice the answer is always -1, 0, or 1.
It is the right tool inside a Comparator. It never overflows, and it works on primitive values, so no boxing is needed.
A static method of java.lang.Integer (since Java 1.7): call it on the class, as Integer.compare(...). Integer lives in java.lang, so no import is needed.
Syntax
Pass the two numbers to compare.
Java
int a = Integer.compare(3, 7); // -1 (3 is less than 7)
int b = Integer.compare(7, 3); // 1
int c = Integer.compare(5, 5); // 0
Call
What it does
Integer.compare(x, y)
Negative, zero, or positive, for less, equal, or greater.
Integer.compare(y, x)
Swapped arguments give descending order in a comparator.
(a, b) -> Integer.compare(a, b)
A lambda comparator for ascending order.
x.compareTo(y)
The same comparison on Integer objects.
Test the result with < 0, == 0, or > 0. Do not rely on the exact value being -1 or 1, even though that is what it returns today.
Library declarations
Java
// java.lang.Integer
public static int compare(int x, int y)
A comparator is a function of two values.List.sort calls it again and again to decide which element comes first.
Normal order.compare(a, b) is negative when a is smaller, so smaller scores move to the front.
Reversed order. Calling compare(b, a) swaps the roles, so the biggest score comes first.
Duplicates stay. The two 95 values compare as 0, so both stay in the list next to each other.
Example 3 — Spot the Overflow Bug in a - b
Compare the old subtraction trick with Integer.compare on a pair of extreme values.
Java
public class IntegerCompareOverflow {
static int bad(int a, int b) {
return a - b;
}
public static void main(String[] args) {
int[][] pairs = {{3, 10}, {Integer.MAX_VALUE, -1}, {Integer.MIN_VALUE, 1}};
for (int[] pair : pairs) {
System.out.println(pair[0] + " vs " + pair[1]
+ ": a - b = " + bad(pair[0], pair[1])
+ ", compare = " + Integer.compare(pair[0], pair[1]));
}
}
}
Output
3 vs 10: a - b = -7, compare = -1
2147483647 vs -1: a - b = -2147483648, compare = 1
-2147483648 vs 1: a - b = 2147483647, compare = -1
How It Works
Small values behave. 3 minus 10 is -7, which has the right sign, while compare says -1.
MAX_VALUE minus -1 overflows. The true answer 2147483648 does not fit in an int, so it wraps to -2147483648, and the code would wrongly say that MAX_VALUE is smaller.
MIN_VALUE minus 1 overflows too. It wraps around to the largest positive number, so the sign is wrong again.
compare is always right. It never subtracts; it only looks at which value is bigger.
Frequently Asked Questions
A negative number if the first int is smaller, 0 if they are equal, and a positive number if the first is larger. In practice it returns -1, 0, or 1.
compare is static and takes two primitive ints. compareTo is called on an Integer object and takes one other Integer. They give the same result.
The subtraction can overflow and give the wrong sign for large or negative values. Integer.compare(a, b) is safe.
Use a comparator that swaps the arguments: list.sort((a, b) -> Integer.compare(b, a)).
Java 7. On older versions write (a < b) ? -1 : (a == b ? 0 : 1) yourself.