Comparator Interface

java.util.Comparator;
public interface Comparator<T>
A comparison function, which imposes a total ordering on some collection of objects. Comparators can be passed to a sort method (such as Collections.sort or Arrays.sort) to allow precise control over the sort order. Comparators can also be used to control the order of certain data structures (such as sorted sets or sorted maps), or to provide an ordering for collections of objects that don't have a natural ordering.
Comparator interface features:
  • Sorting logic is in separate class. Hence we can write different sorting based on different attributes of objects to be sorted. E.g. Sorting using id,name etc.
  • int compare(Object o1,Object o2)
    This method compares o1 and o2 objects. and returns a integer.Its value has following meaning.
    1. positive – o1 is greater than o2
    2. zero – o1 equals to o2
    3. negative – o1 is less than o1
Method of Comparator interface
  1. public int compare(T o1, T o2);
    Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
  2. public boolean equal(Object obj);
    This method is used for equality tested of object . The method returns true if obj and the invoking object are both Comparator objects and use the same ordering. Otherwise, it returns false. Overriding equals( ) is unnecessary, and most simple comparators will not do so.
Example of Comparator Interface
Employee.java
					 
import java.lang.Comparable; public class Employee implements Comparable{ int employeeId; String employeeName; int employeeAge; public Employee(int employeeId, String employeeName, int employeeAge) { super(); this.employeeId = employeeId; this.employeeName = employeeName; this.employeeAge = employeeAge; } public int compareTo(Object arg0) { Employee employee=(Employee) arg0; return (this.employeeId < employee.employeeId ) ? -1: (this.employeeId > employee.employeeId ) ? 1:0 ; } public int getEmployeeId() { return employeeId; } public void setEmployeeId(int employeeId) { this.employeeId = employeeId; } public String getEmployeeName() { return employeeName; } public void setEmployeeName(String employeeName) { this.employeeName = employeeName; } public int getEmployeeAge() { return employeeAge; } public void setEmployeeAge(int employeeAge) { this.employeeAge = employeeAge; } }
 
ComparatorExample.java
					 
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class ComparatorExample{ public static void main(String[] args) { Employee emp1=new Employee(131, "Sunil",30); Employee emp2=new Employee(121, "Anand",22); Employee emp3=new Employee(123, "Sujeet",27); Employee emp4=new Employee(104, "Sanjeev",28); List<Employee> list = new ArrayList<Employee>(); list.add(emp1); list.add(emp2); list.add(emp3); list.add(emp4); System.out.println("Before Sort by id : "); for (int i = 0; i < list.size(); i++) { Employee employee=(Employee) list.get(i); System.out.println("Employee Id: "+employee.getEmployeeId()+ " || "+"Employee name: "+employee.getEmployeeName()+ " || "+"Employee age: "+employee.getEmployeeAge()); } //Sort by EmployeeId Collections.sort(list,new Comparator<Employee>() { public int compare(Employee e1, Employee e2) { return (e1.getEmployeeId() < e2.getEmployeeId()) ? -1: (e1.getEmployeeId() > e2.getEmployeeId() ) ? 1:0 ; } }); System.out.println("After Sort by id: "); for (int i = 0; i < list.size(); i++) { Employee employee=(Employee) list.get(i); System.out.println("Employee Id: "+employee.getEmployeeId()+ "|| "+"Employee name: "+employee.getEmployeeName()+ " || "+"Employee age: "+employee.getEmployeeAge()); } //Sort by EmployeeName Collections.sort(list,new Comparator<Employee>() { public int compare(Employee e1, Employee e2) { return e1.getEmployeeName().compareTo(e2.getEmployeeName()); } }); System.out.println("After Sort by name: "); for (int i = 0; i < list.size(); i++) { Employee employee=(Employee) list.get(i); System.out.println("Employee Id: "+employee.getEmployeeId()+ "|| "+"Employee name: "+employee.getEmployeeName()+ " || "+"Employee age: "+employee.getEmployeeAge()); } } }
Output

Before Sort :
Employee Id: 131||Employee name: Sunil||Employee Age: 30
Employee Id: 121||Employee name: Anand||Employee Age: 22
Employee Id: 123||Employee name: Sujeet||Employee Age: 27
Employee Id: 104||Employee name: Sanjeev||Employee Age: 28
After Sort by Id :
Employee Id: 104|| Employee name: Sanjeev||Employee Age: 28
Employee Id: 121|| Employee name: Anand||Employee Age: 22
Employee Id: 123|| Employee name: Sujeet||Employee Age: 27
Employee Id: 131|| Employee name: Sunil||Employee Age: 30

Download this example
 



 

Latest Tutorial


Most Visited Tutorial