In this post under Java Collections, I will show an example of how to use Comparable interface when we need to sort collections on multiple fields
For our example, we will sort a collections of employee objects.
I need to sort the collections of employee objects in ascending order first using there name and then there age
The structure of Employee class is as shown below
Employee
package core.collection.package33;public class Employee implements Comparable<Employee> { private int id; private String name; private boolean status; private int age; public Employee(int id, String name, boolean status, int age) { this.id = id; this.name = name; this.status = status; this.age = age; } @Override public int compareTo(Employee employee) { int comparisonResult = this.name.compareTo(employee.name); if(comparisonResult != 0) return comparisonResult; return Integer.compare(this.age, employee.age); } @Override public String toString() { return "Employee{" + "id=" + id + ", name='" + name + '\'' + ", status=" + status + ", age=" + age + '}'; }}
As you can see, the above class implements the “Comparable” interface and provides implementation for “compareTo” method.
The logic in “compareTo” method is of importance.
First we compare the employee’s name field and store its result in “comparisonResult” variable.
Next we check whether “comparisonResult” is not equal to 0. If yes, we return the “comparisonResult” value.
If “comparisonResult” is equal to 0, we are then comparing the age of the employee and then the result of that comparison is returned.
So basically what we are doing is first we sort the employee collections by name in ascending order. In case if two employee objects has same name then we are sorting them by age.
Below is the main class for your reference
Main class
package core.collection.package33;import java.util.ArrayList;import java.util.Collections;import java.util.List;public class Example33 { public static void main(String[] args) { Employee employee1 = new Employee(1, "Jack Daniel", true, 40); Employee employee2 = new Employee(2, "Jack Daniel", true, 50); Employee employee3 = new Employee(3, "Sylvia Plath", true, 60); List<Employee> employeeList = new ArrayList<>(0); employeeList.add(employee3); employeeList.add(employee2); employeeList.add(employee1); System.out.println("Before sorting: " + employeeList); Collections.sort(employeeList); System.out.println("After sorting: " + employeeList); }}
Below is the output
Output
Before sorting: [Employee{id=3, name='Sylvia Plath', status=true, age=60}, Employee{id=2, name='Jack Daniel', status=true, age=50}, Employee{id=1, name='Jack Daniel', status=true, age=40}]After sorting: [Employee{id=1, name='Jack Daniel', status=true, age=40}, Employee{id=2, name='Jack Daniel', status=true, age=50}, Employee{id=3, name='Sylvia Plath', status=true, age=60}]
As you can see from the output two employees have same name “Jack Daniel”, so we sort them using there age. Then we see employee by name “Sylvia Plath” as last object in the collection.
In this way we sort collections on multiple fields using Comparable interface