I'm tyring to sort an array of items in which an item should be brought to the beginning of the array if its id equals the id of a selected item (also belonging to the array). The sorting order of the rest of the elements is their distance from a given location.
I'm experiencing the dreaded java.lang.IllegalArgumentException: Comparison method violates its general contract!
exception in my custom comparator, implemented like this:
Location location = ...; // may be null
Item selectedItem = ...; // may be null or an element of the array to be sorted
private final Comparator<Item> comparator = new Comparator<Item>() {
@Override
public int compare(Item p1, Item p2) {
// if p1 is the currently selected item, bring p1 to the top
// if p2 is the currently selected item, bring p2 to the top
// else sort p1 and p2 by their distance from location
if (selectedItem != null) {
if (selectedItem.getId() == p1.getId()) { //id's are int and unique in the array
return -1;
} else if (selectedItem.getId() == p2.getId()) {
return 1;
}
}
if (location != null) { //location is an Android Location class instance
Float distance1 = location.distanceTo(p1.getLocation());
Float distance2 = location.distanceTo(p2.getLocation());
return distance1.compareTo(distance2);
} else {
return 0;
}
}
};
I don't have an exact sequence to replicate the issue, but all observations of the error so far happen when selectedItem
and location
are not null (they may be both null).
Any hints?
Thanks