Java 6 equivalent of Integer.compare

This is specified in the doc :

Compares two int values numerically. The value returned is identical to what would be returned by: Integer.valueOf(x).compareTo(Integer.valueOf(y))

So you can use :

Integer.valueOf(x).compareTo(Integer.valueOf(y))

How do create a similar function in Java 6?

The source is open and you can find the implementation here.

public static int compare(int x, int y) {
      return (x < y) ? -1 : ((x == y) ? 0 : 1);
}

You can use the implementation present in the Java 7 implementation

public static int compare(int x, int y) {
    return (x < y) ? -1 : ((x == y) ? 0 : 1);
}

For your java 6 project, you wrap this in a utility class, and remove that class once you migrate to Java7

Tags:

Java