0

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.

8
  • see this answer may be helpfull stackoverflow.com/a/3705372/7328984 Commented Apr 12, 2018 at 7:18
  • 1. Comparator of Profile is comparing Emp1 and Emp2 class objects 2. What is the experience attribute type is not specificed. Commented Apr 12, 2018 at 7:19
  • How will that be a Comparator<Profile> when you're comparing Employees? Commented Apr 12, 2018 at 7:19
  • 1
    java.lang.Float already implements Comparable, any reason you want to do it this way. And not reverse compare the two values? Commented Apr 12, 2018 at 7:24
  • 1
    Math.round(t.getExperience() - t1.getExperience()) does not work because it will consider 5.3 and 5.0 equal, due to rounding. This trick only works with integers. Commented Apr 12, 2018 at 7:33

2 Answers 2

10

Math.round(t.getExperience() - t1.getExperience()) doesn't compare the two numbers, so I don't know what you were expecting to achieve.

You are supposed to use:

Collections.sort(employeeList, new Comparator<Emp>() {
    @Override
    public int compare(Emp t, Emp t1) {
        return Float.compare(t1.getExperience(), t.getExperience());
    }
});

Note that the parameters passed to Float.compare are in the opposite order compared to the parameters of the wrapping compare method, which will produce sorting by descending order.

Sign up to request clarification or add additional context in comments.

9 Comments

Maybe he wanted Comparator.comparing(e -> Math.round(e.getExperience()))?
@daniu the posted example shows regular descending order, so it doesn't look like Math.round() is required.
When I tried above it is saying compare (float, float) in Float cannot be applied to (float) @Eran
@ShylendraMadda you didn't try the above. Learn the difference between one parameter and two.
@ShylendraMadda - and , are not the same
|
8

You can use Comparator.comparing

 employeeList.sort(Comparator. comparingDouble(Employee::getExperience).reversed());

But it is advised not to mutate existing collections, but to create a new one instead. So with a stream, as an example:

Comparator<Employee> byExperience = Comparator. comparingDouble(Employee::getExperience).reversed(); 
List<Employee> employeeList = ...;
List<Employee> sortedByExperienceEmployeeList =
 employeeList.sorted(byExperience)
.toList();

It will produce:

5.6 5.5 5.0 4.5 3.3

1 Comment

Better use Comparator.comparingDouble. Avoids unnecessary boxing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.