How to sort ArrayList with objects over two parameters



  • Got a class. Employee in which ArrayList<Employee>, we need to separate staff on the list. ЗПif ЗП The same name. How to quarry ЗП I know, but here's how to quarry at the same time. ЗП I can't understand a name. Thank you. You can at least give me a clue.



  • Let's say we have class.

    class Employee {
        String name;
        double salary;
    }
    

    and variables

    ArrayList<Employee> employees;
    

    For example, it is possible to sort:

    Collections.sort(
        employees,
        new Comparator<Employee>() {
            public int compare(final Employee e1, final Employee e2) {
                if (e1.salary < e2.salary)
                    return -1;
                if (e2.salary > e2.salary)
                    return 1;
                return e1.name.compareTo(e2.name);
            }
        }
    );
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2