Table of Contents
In this post, we will see how you can use comparator to sort list of objects in java.
Comparator:
When you want to sort the list of objects of a class,you can use Comparator interface. You don’t need to implement Comparator on the class whose objects need to be sorted. You can create a separate class and implement a Comparator interface as below.
For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package com.arpit.java2blog; import java.util.Comparator; public class EmployeeSortByIdComparator implements Comparator{ @Override public int compare(Employee e1, Employee e2) { return e1.getEmpId()-e2.getEmpId(); } } |
You can use different sorting logic based on different attributes of object that needs to be sorted.
For example:
Let’s say you want to sort list of employees by name,you can use below ocde to do that.
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 |
Employee e1= new Employee(4, "John", 20); Employee e2= new Employee(3, "Martin", 40); Employee e3= new Employee(1, "Mary", 28); Employee e4= new Employee(2, "Andrew", 35); List<Employee> listofEmployees=new ArrayList<>(); listofEmployees.add(e1); listofEmployees.add(e2); listofEmployees.add(e3); listofEmployees.add(e4); System.out.println("Before Sorting by name: "); for (Employee e:listofEmployees) { System.out.println("Employee Id: "+e.getEmpId()+"|| name: "+e.getName()); } Collections.sort(listofEmployees,new Comparator<Employee>() { @Override public int compare(Employee o1, Employee o2) { return o1.getName().compareTo(o2.getName()); } }); System.out.println("After Sorting by name: "); for (Employee e:listofEmployees) { System.out.println("Employee Id: "+e.getEmpId()+"|| name: "+e.getName()); } |
Java code for Comparator:
Create a class named Employee.java which will have empId, name and age.
1.Employee.java
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.arpit.java2blog; public class Employee { private int empId; private String name; private int age; public Employee(int empId, String name, int age) { super(); this.empId = empId; this.name = name; this.age = age; } public int getEmpId() { return empId; } public void setEmpId(int empId) { this.empId = empId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } |
Create a class named “EmployeeSortByIdComparator”. This class will have logic to sort list of Employees by empId.
2.EmployeeSortByIdComparator.java
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package com.arpit.java2blog; import java.util.Comparator; public class EmployeeSortByIdComparator implements Comparator{ @Override public int compare(Employee e1, Employee e2) { return e1.getEmpId()-e2.getEmpId(); } } |
Let’s create main class which will have logic to create a list of objects and sort it based on empId.
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 |
package com.arpit.java2blog; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class EmployeeComparatorMain { public static void main(String[] args) { Employee e1= new Employee(4, "John", 20); Employee e2= new Employee(3, "Martin", 40); Employee e3= new Employee(1, "Mary", 28); Employee e4= new Employee(2, "Andrew", 35); List<Employee> listofEmployees=new ArrayList<>>(); listofEmployees.add(e1); listofEmployees.add(e2); listofEmployees.add(e3); listofEmployees.add(e4); System.out.println("Before Sorting by empId: "); for (Employee e:listofEmployees) { System.out.println("Employee Id: "+e.getEmpId()+"|| name: "+e.getName()); } Collections.sort(listofEmployees,new EmployeeSortByIdComparator()); System.out.println("After Sorting by empId: "); for (Employee e:listofEmployees) { System.out.println("Employee Id: "+e.getEmpId()+"|| name: "+e.getName()); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 |
Before Sorting by empId: Employee Id: 4|| name: John Employee Id: 3|| name: Martin Employee Id: 1|| name: Mary Employee Id: 2|| name: Andrew After Sorting by empId: Employee Id: 1|| name: Mary Employee Id: 2|| name: Andrew Employee Id: 3|| name: Martin Employee Id: 4|| name: John |
Anonymous Comparator:
One of advantage of Comparator over comparable is you can create anonymous comparator i.e you don’t need to implement Comparable interface to class whose objects need to be sorted.
Let’s understand more with help of example:
We will use an anonymous class to sort the list of Employees by name.
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 |
package com.arpit.java2blog; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class EmployeeComparatorMain { public static void main(String[] args) { Employee e1= new Employee(4, "John", 20); Employee e2= new Employee(3, "Martin", 40); Employee e3= new Employee(1, "Mary", 28); Employee e4= new Employee(2, "Andrew", 35); List<Employee> listofEmployees=new ArrayList<>>(); listofEmployees.add(e1); listofEmployees.add(e2); listofEmployees.add(e3); listofEmployees.add(e4); System.out.println("Before Sorting by name: "); for (Employee e:listofEmployees) { System.out.println("Employee Id: "+e.getEmpId()+"|| name: "+e.getName()); } Collections.sort(listofEmployees,new Comparator<Employee>() { @Override public int compare(Employee o1, Employee o2) { return o1.getName().compareTo(o2.getName()); } }); System.out.println("After Sorting by name: "); for (Employee e:listofEmployees) { System.out.println("Employee Id: "+e.getEmpId()+"|| name: "+e.getName()); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 |
Before Sorting by name: Employee Id: 4|| name: John Employee Id: 3|| name: Martin Employee Id: 1|| name: Mary Employee Id: 2|| name: Andrew After Sorting by name: Employee Id: 2|| name: Andrew Employee Id: 4|| name: John Employee Id: 3|| name: Martin Employee Id: 1|| name: Mary |
that’s all about Comparator in Java.
You may also like: