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
}
}