1

I have a block of data, and I want to sort them according to age and salary.

Object Employee has:

  • Name
  • Designation
  • Age
  • Salary

I want to sort then so that the max age will be on top. But if the age is same of two persons,then it will compare the salary between them, and then for whom Age and Salary is high, he will be on top of the ArrayList.

Here in the top list accordingly: Bob, Tim, Tom. then in the list: Alex, Sam, Craig and so on.

List<Employee> listEmployees = new ArrayList<Employee>(); 
listEmployees.add(new Employee("Tom", "Developer", 45, 80000));
listEmployees.add(new Employee("Sam", "Designer", 30, 75000));
listEmployees.add(new Employee("Bob", "Designer", 45, 134000));
listEmployees.add(new Employee("Peter", "Programmer", 25, 60000));
listEmployees.add(new Employee("Tim", "Designer", 45, 130000));
listEmployees.add(new Employee("Craig", "Programmer", 30, 52000));
listEmployees.add(new Employee("Anne", "Programmer", 25, 51000));
listEmployees.add(new Employee("Alex", "Designer", 30, 120000));

I have tried using

Collections.sort(Employees , new Comparator<listEmployees >() {
@Override
public int compare(EmployeeModelCls lhs, EmployeeModelCls rhs) {
return (rhs.getAge() - lhs.getAge());
}
});

also I have tried

Collections.sort(Employees , new Comparator<listEmployees >() {
@Override
public int compare(EmployeeModelCls lhs, EmployeeModelCls rhs) {
return ((rhs.getAge() - lhs.getAge()) - (rhs.getSalary() - lhs.getSalary()));
}
});

but unable to get correct data. Does anybody have any ideas?

4
  • First, share after age, then sort after name Commented Jun 16, 2014 at 12:33
  • why you use -? Try to use compareTo(...) Commented Jun 16, 2014 at 12:34
  • Those sorting code blocks compile or am I missing something? Commented Jun 16, 2014 at 12:35
  • have you store data in database ? Commented Jun 16, 2014 at 12:37

3 Answers 3

2

Make the comparator as natural as possible, because it gives you clear idea how the employees are compared. For example,

Collections.sort(Employees , new Comparator<listEmployees >() {
  public int compare(EmployeeModelCls lhs, EmployeeModelCls rhs) {
    if (lhs.getAge() < rhs.getAge()) {
      return 1;
    } else if (lhs.getAge() > rhs.getAge()) {
      return -1;
    } else {
      if (lhs.getSalary() < rhs.getSalary()) {
        return 1;
      } else if (lhs.getSalary() > rhs.getSalary()) {
        return -1;
      } else {
        return 0;
      }
    }
  }
});

Once you do this, you can optimize the comparator. For example,

Collections.sort(Employees , new Comparator<listEmployees >() {
  public int compare(EmployeeModelCls lhs, EmployeeModelCls rhs) {
    return (rhs.getAge() == lhs.getAge()) ? (rhs.getSalary() - lhs.getSalary()) : (rhs.getAge() - lhs.getAge());
  }
});

As you can see, we compare the salaries only when the ages are the same. In your code, you compare the salaries even when the ages are not the same. This is the problem.

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

Comments

1

Consider using guava's ordering utilities to create a compound comparator. In this specific case, it appears you need 2 comparators compounded together, first age then salary.

Comments

0

Maybe try this way

Collections.sort(listEmployees , new Comparator<listEmployees >() {
    @Override
    public int compare(EmployeeModelCls lhs, EmployeeModelCls rhs) {
        if (rhs.getAge() - lhs.getAge() == 0)
            return rhs.getSalary() - lhs.getSalary();

        return (rhs.getAge() - lhs.getAge());
    }
});

Comments

Your Answer

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