In this article we will see the sorting of arraylist in java using comparators. This is mainly used for sorting arraylist that contain user defined objects.
Collection.sort()
takes 2 parameters.
1st parameter is the list that needs to be sorted.
2nd parameter is the object of the class that implements the Comparator.
For the comparator to perform its job we need to implement the compareTo
method.
Collection.sort() internal uses the compareTo method and decides the sorted list.
Lets create a object named Employee
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
package com.kscodes.sampleproject; public class Employee { private String name; private long salary; private int yearsOfExp; public Employee(String name, long salary, int yearsOfExp) { super(); this.name = name; this.salary = salary; this.yearsOfExp = yearsOfExp; } public String getName() { return name; } public void setName(String name) { this.name = name; } public long getSalary() { return salary; } public void setSalary(long salary) { this.salary = salary; } public int getYearsOfExp() { return yearsOfExp; } public void setYearsOfExp(int yearsOfExp) { this.yearsOfExp = yearsOfExp; } } |
Now to sort a list that contains Employee ,based on the salary we need to write a Comparator implementation as below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.kscodes.sampleproject; import java.util.Comparator; public class SalaryComparator implements Comparator<Employee> { public int compare(Employee o1, Employee o2) { if (o1.getSalary() > o2.getSalary()) { return 1; } return -1; } } |
Please Note:
The return value of the compare method decides the equality of the objects.
If return value is a negative number – Object 1 is less than Object 2
If return value is a positive number – Object 1 is greater than Object 2
If return value is 0 – Object 1 is equal to Object 2
Now we will use the SalaryComparator class to sort the list
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
package com.kscodes.sampleproject; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class ArrayListSort { public static void main(String[] args) { List<Employee> employeeList = new ArrayList<Employee>(); employeeList.add(new Employee("John", 4000, 2)); employeeList.add(new Employee("Scott", 8700, 5)); employeeList.add(new Employee("Stuart", 5500, 3)); employeeList.add(new Employee("Bob", 1000, 1)); System.out.println("*****Before Sorting*****"); printAllEmployeeDetails(employeeList); Collections.sort(employeeList, new SalaryComparator()); System.out.println("*****After Sorting*****"); printAllEmployeeDetails(employeeList); } public static void printAllEmployeeDetails(List<Employee> employeeList) { for (Employee e : employeeList) { System.out.println(e.getName() + "::" + e.getSalary()); } } } |
OutPut
*****Before Sorting*****
John::4000
Scott::8700
Stuart::5500
Bob::1000
*****After Sorting*****
Bob::1000
John::4000
Stuart::5500
Scott::8700
Sorting of ArrayList using multiple Comparators
Instead of creating a new class for Comparator, we can directly create a instance of the Comparator interface and use it in the sort method as shown in below example.
We can use this to create multiple sort alogrithm.
In the below example we are creating 2 sort alogo’s – on Salary and on Experience
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
package com.kscodes.sampleproject; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class ArrayListSort { public static void main(String[] args) { List<Employee> employeeList = new ArrayList<Employee>(); employeeList.add(new Employee("John", 4000, 2)); employeeList.add(new Employee("Scott", 8700, 5)); employeeList.add(new Employee("Stuart", 5500, 10)); employeeList.add(new Employee("Bob", 1000, 12)); System.out.println("*****Before Sorting*****"); printAllEmployeeDetails(employeeList); Collections.sort(employeeList, new Comparator<Employee>() { public int compare(Employee o1, Employee o2) { if (o1.getSalary() > o2.getSalary()) { return 1; } return -1; } }); System.out.println("*****After Sorting Based on Salary*****"); printAllEmployeeDetails(employeeList); Collections.sort(employeeList, new Comparator<Employee>() { public int compare(Employee o1, Employee o2) { if (o1.getYearsOfExp() > o2.getYearsOfExp()) { return 1; } return -1; } }); System.out .println("*****After Sorting Based on Years of Experience****"); printAllEmployeeDetails(employeeList); } public static void printAllEmployeeDetails(List<Employee> employeeList) { for (Employee e : employeeList) { System.out.println(e.getName() + ":: Salary - " + e.getSalary() + " :: Exp -" + e.getYearsOfExp()); } } } |
Output
*****Before Sorting*****
John:: Salary – 4000 :: Exp -2
Scott:: Salary – 8700 :: Exp -5
Stuart:: Salary – 5500 :: Exp -10
Bob:: Salary – 1000 :: Exp -12
*****After Sorting Based on Salary*****
Bob:: Salary – 1000 :: Exp -12
John:: Salary – 4000 :: Exp -2
Stuart:: Salary – 5500 :: Exp -10
Scott:: Salary – 8700 :: Exp -5
*****After Sorting Based on Years of Experience****
John:: Salary – 4000 :: Exp -2
Scott:: Salary – 8700 :: Exp -5
Stuart:: Salary – 5500 :: Exp -10
Bob:: Salary – 1000 :: Exp -12