2

I have an ArrayList with some objects that contains 3 parameters (id, name, salary). Unsorted list:

public class Employee {
        private int id;
        private String name;
        private double salary;

        public Employee(int _id, String _name, double _salary) {
            this.id = _id;
            this.name = _name;
            this.salary = _salary;
        }

        public int getEmployeeID () {
            return this.id;
        }


        public String getEmployeeName () {
            return this.name;
        }

        public double getMonthSalary() {
            return this.salary;
        }

        public static void main(String[] argc) {
            ArrayList<Employee> list = new ArrayList<Employee>();
            list.add(new Employee(1, "asd", 1300));
            list.add(new Employee(7, "asds", 14025)); // this
            list.add(new Employee(2, "nikan", 1230));
            list.add(new Employee(3, "nikalo", 12330));
            list.add(new Employee(8, "aaaa", 14025)); // and this are equal
            list.add(new Employee(4, "nikaq", 140210));
            list.add(new Employee(5, "nikas", 124000)); 
            list.add(new Employee(6, "nikab", 14020));
        }
}

And I want to sort list by salary, and if salaries are equal then sort their names alphabetically. I tried to use Collections.sort();, but it sorts by all of arguments.

e.g: 14025 and 14025 are equal, so sort this one by name: asds, aaaa --> aaaa, asds

Here's what I tried:

for (int i = 0; i < list.size() - 1; i++) {
    for (int j = 0; j < list.size() - 1; j++) {
        boolean isSorted = false;
        if (list.get(j + 1).getMonthSalary() > list.get(j).getMonthSalary()) {
            Employee temprary = list.get(j);
            list.set(j, list.get(j + 1));
            list.set(j + 1, temprary);
            isSorted = true;
        }

        if (isSorted) {
            if (list.get(j).getMonthSalary() == list.get(j + 1).getMonthSalary()) {
                Collections.sort(list, new Comparator < Employee > () {
                    @Override
                    public int compare(Employee s1, Employee s2) {
                        return s1.getEmployeeName().compareToIgnoreCase(s2.getEmployeeName());
                    }
                });
            }
        }
    }
}
2
  • can you use java 8?? Commented Jun 25, 2016 at 21:29
  • 1
    @ΦXocę웃Пepeúpa thanks, Mureinik already answered my question Commented Jun 26, 2016 at 6:42

2 Answers 2

6

Java has a built-in mechanism to handle such a problem - Comparators. You shouldn't reinvent the wheel:

list.sort(Comparator.comparing(Employee::getMonthSalary)
                    .thenComparing(Employee::getEmployeeName));
Sign up to request clarification or add additional context in comments.

4 Comments

So what if I have another constructor from another class that extends Employee.java? e.g Employee2(int id, String name, double salary) How to sort ArrayList<Employee> that contains Employee.add(new Employee2(1, "test", 1000);?
If Employee2 extends Employee, it's still a type of Employee and the same code will work for it too.
I get an error: Employee.sort(Comparator.comparing(Employee::getEmployeeSalary) and etc - Cannot resolve method GetMonthSalary, Cannot resolve method getEmployeeName. But if Employee.sort(Comparator.comparing(Employee2::getEmployeeSalary) and etc it works! What's wrong?
@phen0men works just fine for me, so there's obviously something missing here. However, it's extremely hard to read code in the comments like this. Could you please post a new question with the entire code and the exact error so it would be easier to answer?
0

Collections.sort() sort by natural ordering. Try passing a comparator that compares the way you want to or implement compareTo.

Comments

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.