In an android application, I have a list of locations. I need to sort them based on their distance with the user location. for that I implemented a custom comparator:
Collections.sort(houseList, new Comparator<HouseEntity>()
{
@Override
public int compare(HouseEntity house1, HouseEntity house2)
{
if(userLocation == null) return 0;
return (int) (userLocation.distanceTo(house1.location) - userLocation.distanceTo(house2.location));
}
});
It is working well on all the testings I made. However some users had a crash with the following error:
java.lang.IllegalArgumentException: Comparison method violates its general contract!
After reading all the other same issues on SO I concluded that this error appears when there might be an error in the logic (for example we might get a>b and b>a in the same time). But I couldn't find any scenario to replicate that logic error.
What scenario could be causing this error? And how can I solve it?
Thanks a lot for any help