Search code examples
javasortingpuzzle

Why is my simple comparator broken?


I have a class, which I have simplified to this:

final class Thing {
    private final int value;
    public Thing(int value) {
        this.value = value;
    }
    public int getValue() {
        return value;
    }
    @Override public String toString() {
        return Integer.toString(value);
    }
}

I want to sort an array of this thing. So I have created a simple copmarator:

private static final Comparator<Thing> reverse = new Comparator<Thing>() {
    public int compare(Thing a, Thing b) {
        return a.getValue() - b.getValue();
    }
};

I then use the two argument form of Arrays.sort.

This works fine for my test cases, but sometimes it goes all wrong with the array ending up in a strange but repeatable order. How can this be?


Solution

  • Integer overflow… or more precisely, underflow.

    Instead, do an explicit comparison:

    private static final Comparator<Thing> reverse = new Comparator<Thing>() {
        public int compare(Thing a, Thing b) {
          int av = a.getValue(), bv = b.getValue();
          return (av == bv) ? 0 : ((av < bv) ? -1 : +1);
        }
    };
    

    Using subtraction is fine if you are sure that the difference won't "wrap around". For example, when the values in question are constrained to be non-negative.