0
votes

I'm seeing a lot of crashes on some Chinese Android phones for my app with the error: Comparison method violates its general contract!

I have read that it is something to do with the Collections.sort call.

The thing I'm not too sure about is if it is because of my custom Comparator or not.

Here is where the error occurs:

            if (!airports.isEmpty()) {
            Collections.sort(airports, new DistanceToAirportComparator(location));
            return airports.get(0);
        }

And the Comparator looks like this:

public class DistanceToAirportComparator implements Comparator<Airport> {

private final Location location;

public DistanceToAirportComparator(Location location) {
    this.location = location;
}

@Override
public int compare(Airport airport1, Airport airport2) {
    Location loc1 = getLocation(airport1);
    Location loc2 = getLocation(airport2);
    return Double.compare(location.distanceTo(loc1), location.distanceTo(loc2));
}

private Location getLocation(Airport airport){
    Location location = new Location(LocationManager.GPS_PROVIDER);
    location.setLatitude(airport.getLocation().getLatitude());
    location.setLongitude(airport.getLocation().getLongitude());
    return location;
}

So I'm not too sure if the Comparator is messing something up or not or if I need to do the collections.sort call in a different way

Appreciate any help

1
Your sort call is correct. The problem might be that you don't compare the two passed objects, but instead compare both to a third one. That's just a guess though. - F43nd1r
If you check the Javadoc's for Comparator or Comparable, you'll see the contract. Test your comparator code separately to see if it's possible to break the contract with it. - Kayaman
Are you referring to loc1 & loc2? When you say 3rd object do you mean returning the Double.compare? - Ayohaych
See @Derek Fung s answer, he explained it better. Your comparison might be unstable, because it is based on a third component, the location saved in the comparator. - F43nd1r
@F43nd1r Thanks. Just not sure what I need to change in the code to fix it. - Ayohaych

1 Answers

1
votes

The exception is talking about something like this:

Comparator should be correct in the sense that, if A > B and B > C, then A > C must be true.

So, it comes down to why it has such problem. My guess is that because of the nature of Location, calculation of the distance is unstable

I would suggest you to pre-calculate the distance and use a Comparator on the result instead.

Edit:

Sample

private Airport sortByDistance(Location location) {
    List<Airport> airports;
    if (!airports.isEmpty()) {
        ArrayList<AirportWithDistance> distanceArrayList = new ArrayList<AirportWithDistance>(airports.size());
        for (Airport airport : airports) {
            distanceArrayList.add(new AirportWithDistance(airport, getDistance(airport, location)));
        }
        // replace start
        Collections.sort(distanceArrayList, new DistanceToAirportComparator());
        return airports.get(0).airport;
        // replace end
        //also seems u should replace the above to the line below instead, as you only wan min
        //return Collections.min(distanceArrayList, new DistanceToAirportComparator()).airport;
    }
    return null;
}


private static double getDistance(Airport airport, Location location){
    Location airportLocation = new Location(LocationManager.GPS_PROVIDER);
    airportLocation.setLatitude(airport.getLocation().getLatitude());
    airportLocation.setLongitude(airport.getLocation().getLongitude());
    return location.distanceTo(airportLocation);
}

public static class AirportWithDistance {
    Airport airport;
    double distance;

    AirportWithDistance(Airport airport, double distance) {
        this.airport = airport;
        this.distance = distance;
    }

}

public static class DistanceToAirportComparator implements Comparator<AirportWithDistance> {

    public DistanceToAirportComparator() {
    }

    @Override
    public int compare(AirportWithDistance airport1, AirportWithDistance airport2) {
        return Double.compare(airport1.distance, airport2.distance);
    }

}