by @kodeazy

what is comparator in java and how it works?

Home » java » what is comparator in java and how it works?
  • Comparator is an interface used to sort objects of user defined class.
  • Comparator provides multiple sorting sequences where as comparable provides singles sorting sequence.
  • It is found in java.util package.
  • It has two methods compare() and equals().
  • Below is an example of how to use compare() method for sorting objects in Comparator interface.

    class SortByName implements Comparator<ComparatorEmployee>{
    public int compare(ComparatorEmployee e1,ComparatorEmployee e2){
        return e1.name.compareTo(e2.name);
    }
    }
  • Above method helps to sort the objects based on name of the employee.here compareTo() method returns negitive,positive, zero values, based on these values objects are swapped.
  • Here e1.name.compareTo(e2.name) allows to print the Objects by sorting in Asc order.To print in Desc order modify it as e2.name.compareTo(e1.name).
  • Below is an Example of using Comparator.
import java.util.*;
class ComparatorEmployee{
    int age;
    int salary;
    String name;
    ComparatorEmployee(int age,int salary,String name){
        this.age=age;
        this.salary=salary;
        this.name=name;

    }
   
}
class SortByName implements Comparator<ComparatorEmployee>{
    public int compare(ComparatorEmployee e1,ComparatorEmployee e2){
        return e1.name.compareTo(e2.name);
    }
}
class SortByage implements Comparator<ComparatorEmployee>{
    public int compare(ComparatorEmployee e1,ComparatorEmployee e2){
        if(e1.age>e2.age)
            return 1;
        else if(e1.age<e2.age)
            return -1;
        else
            return 0;
    }
}
class ComparatorExample{
    public static void main(String args[]){
        List<ComparatorEmployee> al=new ArrayList<ComparatorEmployee>();
        al.add(new ComparatorEmployee(27, 103, "Suresh"));
        al.add(new ComparatorEmployee(23, 102, "Ramesh"));
        al.add(new ComparatorEmployee(21, 105, "Mahesh"));
        al.add(new ComparatorEmployee(29, 104, "Gopi"));
        System.out.println("Sorting by Name");
        Collections.sort(al,new SortByName());
        al.forEach(emplyeDetails->{
            System.out.println("Name is:"+emplyeDetails.name+" Age is :"+emplyeDetails.age+"Salary is"+emplyeDetails.salary);
        });
        System.out.println("---------------------------------------------------------");
        System.out.println("Sorting by Age");
        Collections.sort(al,new SortByage());
        al.forEach(emplyeDetails->{
            System.out.println("Name is:"+emplyeDetails.name+" Age is :"+emplyeDetails.age+"Salary is"+emplyeDetails.salary);
        });
    }
}

Output:

Sorting by Name
Name is:Gopi Age is :29Salary is104
Name is:Mahesh Age is :21Salary is105
Name is:Ramesh Age is :23Salary is102
Name is:Suresh Age is :27Salary is103
---------------------------------------------------------
Sorting by Age
Name is:Mahesh Age is :21Salary is105
Name is:Ramesh Age is :23Salary is102
Name is:Suresh Age is :27Salary is103
Name is:Gopi Age is :29Salary is104

To understand working of custom comparato click here