title: Android: Comparison method violates its general contract
body: <p>I'm seeing a lot of crashes on some Chinese Android phones for my app with the error: Comparison method violates its general contract!</p>
  
  <p>I have read that it is something to do with the Collections.sort call.</p>
  
  <p>The thing I'm not too sure about is if it is because of my custom Comparator or not.</p>
  
  <p>Here is where the error occurs:</p>
  
  <pre><code>            if (!airports.isEmpty()) {
              Collections.sort(airports, new DistanceToAirportComparator(location));
              return airports.get(0);
          }
  </code></pre>
  
  <p>And the Comparator looks like this:  </p>
  
  <pre><code>public class DistanceToAirportComparator implements Comparator&lt;Airport&gt; {
  
  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;
  }
  </code></pre>
  
  <p>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</p>
  
  <p>Appreciate any help</p>