So I've got a list announceList which have a list of Announces, I'm trying to sort the list ascending for a percentage value, so if I got the list this way:
Announce1 - 20.10%
Announce2 - 20.54%
Announce3 - 27.50%
Announce4 - 76.36%
After the sorting I want to have the list like this:
Announce4 - 76.36%
Announce3 - 27.50%
Announce2 - 20.54%
Announce1 - 20.10%
The method I'm using is this:
Collections.sort(announceList, new Comparator<Announce>() {
@Override
public int compare(Announce o1, Announce o2) {
int i1 = o1.getMatchPercentage().intValue();
int i2 = o2.getMatchPercentage().intValue();
return Integer.compare(i1, i2);
}
});
adapter.setAnnounceList(announceList);
adapter.notifyDataSetChanged();
But I don't get the list how I wanted, please if you know what I'm I doing wrong tell me, any help will be apreciated, thank you.
intValue()to compare two float/double values?return Integer.compare(i2, i1);Integer.comparecall, then you will sort by largest to smallest.return o2.getMatchPercentage().compareTo(.getMatchPercentage());