0

I want to sort my ArrayList alphabetically. Therefore I wrote a sortEmployees method. In case my employees have the same name I'd like to sort them based on their salary, which means that the employee with the higher salary should be printed out first.

Well, I successfully managed to sort them alphabetically, but I don't know how I can compare, if employees have the same name and if that is the case, the employee with the higher salary shall be printed. Thanks.

ArrayList<Employee> employees = new ArrayList<Employee>();

public void sortEmployees(){
    Collections.sort(employees, (p1, p2) -> p1.name.compareTo(p2.name));
    for(Employee employee: employees){
        System.out.println("ID: " + employee.ID + END_OF_LINE + "Name: "+employee.name + END_OF_LINE + "Salary: " + employee.grossSalary);
        System.out.println(""); // just an empty line
    }
}
3
  • You're almost there. Modify your comparator such that If compareTo() is non-zero use this value, otherwise compare the salaries and use that value. Commented Oct 10, 2017 at 23:51
  • Sorry I didn't quite get it. Do you mind showing me that in code? Commented Oct 11, 2017 at 0:02
  • Comparator.comparing(e -> e.name).thenComparingInt(e -> e.grossSalary) Commented Oct 11, 2017 at 0:24

2 Answers 2

2

Create an appropiate Comparator that will compare two items according to your desired criteria. Then use Collections.sort() on your ArrayList.

If at a later time you want to sort by different criteria, call Collections.sort() again with a different Comparator.

Reference: How to sort an ArrayList using multiple sorting criteria?

Just an advice for future questions, please google properly or search on stackoverflow for your problem before asking a new question. Most of the time such trivial questions have already been asked and answered.

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

1 Comment

@JavaTeachMe2018 Did you figure it out or do you want me to edit my answer?
1

I assume that you have the method equals to return whether two names are equal. You can define your Comparator as follows:

ArrayList<Employee> employees = new ArrayList<Employee>();

public void sortEmployees(){
  Collections.sort(employees, new Comparator<Employee>() {
    @Override 
    public int compare(Employee a, Employee b) {
      if (a.name.equals(b.name)) {
        return b.salary - a.salary;
      } else {
        return p1.name.compareTo(p2.name);
      }
    }
  });
  for(Employee employee: employees){
    System.out.println("ID: " + employee.ID + END_OF_LINE + "Name: "+employee.name + END_OF_LINE + "Salary: " + employee.grossSalary);
    System.out.println(""); // just an empty line
  }
}

1 Comment

While this looks correct, I think it would be better to use a lambda, rather than create a Comparator. It's less code and fits in with the OP's current implementation.

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.