I have a list of employees which had different experiences like
5.0,3.3,5.5,5.6,4.5 etc..
When I am trying to sort max to min experience by using Math.round it's giving the wrong result like:
5.6,5.0,5.5,5.3,4.5 etc..
I want the result like:
5.6,5.5,5.3,5.0,4.5 etc..
Here I used Collections.sort like:
Collections.sort(employeeList, new Comparator<Emp>() {
@Override
public int compare(Emp t, Emp t1) {
return Math.round(t.getExperience() - t1.getExperience()); // which giving wrong results
// return Float.compare(t.getExperience() - t1.getExperience()); // which is not working
}
});
Here t1.getExperience() will give you float result.
Comparator<Profile>when you're comparingEmployees?Math.round(t.getExperience() - t1.getExperience())does not work because it will consider5.3and5.0equal, due to rounding. This trick only works with integers.