I have comparison method for users list. I am moving users up and down in this list based on 3 values: their type, if they are checked as favourite and based on their names and surnames. My method looks like this:
public int compare(ContactEntity lhs, ContactEntity rhs) {
Integer lType = ContactUtils.getPresenceStatusType(lhs.getPresenceStatus(), presenceEntities).getType();
Integer rType = ContactUtils.getPresenceStatusType(rhs.getPresenceStatus(), presenceEntities).getType();
Boolean lhsFavourite = ContactsHelper.isContactFavourite(lhs.getContactId(), favourites);
Boolean rhsFavourite = ContactsHelper.isContactFavourite(rhs.getContactId(), favourites);
int c;
c = lType.compareTo(rType);
if (c == 0)
c = rhsFavourite.compareTo(lhsFavourite);
if (c == 0)
c = lhs.compareTo(rhs);
return c;
}
In very rare cases, when for some users there is very huge friends list, i see in crashlytics error Comparison method violates its general contract
. What is wrong with my code?
ContactEntity comparison method:
@Override
public int compareTo(@NonNull ContactEntity another) {
int compared = mFirstName.compareTo(another.getFirstName());
if (compared == 0) {
compared = mLastName.compareTo(another.getLastName());
}
return compared;
}