Comparable and Comparator continue…

N:B: If you have not gone through my previous post about comparator then once go through the link Comparable & Comparator: then it will be easy to understand this topic.

Comparator:

  • Java Comparator interfaceis used to order the objects of user-defined class.
  • It is basically used for custom sorting, that means you can sort employee objects by name by salary.
  • Comparator interface is in java.util package and its having one method compare() which takes two user defined objects as argument.

public int compare(Object obj1,Object obj2)

  • Compare() method compares first object data member with second object data member and its return type is “int”.
  • While shorting through Collections.sort() method we have to pass the Collection object as well as the Comparator object.
  • Let me discuss it though examples.

Employee.java:

package com.tcoj.model;

 

public class Employee {

int id;

public String name;

public double salary;

@Override

public String toString() {

 

return “[“+id+”,”+name+”,”+salary+”]”;

}

public Employee(int id, String name, double salary) {

super();

this.id = id;

this.name = name;

this.salary = salary;

}

}

  • Now Let me write two comparators one as NameComparator another SalaryComparator for two custom comparing.
  • The Idea here is to compare the Employee objects through different fields.

NameComparator.java:

import java.util.Comparator;

import com.tcoj.model.Employee;

 

public class NameComparator implements Comparator<Employee> {

 

@Override

public int compare(Employee emp1, Employee emp2) {

 

return emp1.name.compareTo(emp2.name);

}

 

}

 

SalaryComarator.java:

import java.util.Comparator;

 

import com.tcoj.model.Employee;

 

public class SalaryComparator implements Comparator<Employee> {

 

@Override

public int compare(Employee emp1, Employee emp2) {

double d=emp1.salary-emp2.salary;

if(d>0)

return 1;

else if(d < 0)

return -1;

else

return 0;

}

}

 

  • Now Let me write the client class where I will add some Employee objects and add them to list.
  • Then after passing two different Comparator objects we will see the sorting.

Client.java:

package com.tcoj.clint;

 

import java.util.ArrayList;

import java.util.Collections;

import java.util.Iterator;

import java.util.List;

 

import com.tcoj.comparators.NameComparator;

import com.tcoj.comparators.SalaryComparator;

import com.tcoj.model.Employee;

 

public class Clint {

 

public static void main(String[] args) {

List<Employee> list=new ArrayList<Employee>();

Employee emp1=new Employee(102, “shaswot”, 11500.89);

Employee emp2=new Employee(100, “subham”, 12500.89);

Employee emp3=new Employee(107, “ashok”, 11500.99);

Employee emp4=new Employee(108, “rahul”, 13501.09);

 

list.add(emp1);

list.add(emp2);

list.add(emp3);

list.add(emp4);

Collections.sort(list,new NameComparator()); //passing namecomparator object

Iterator<Employee> itr=list.iterator();

System.out.println(“Sort by name=============================”);

while(itr.hasNext())

{

System.out.println(itr.next());

}

System.out.println(“Sort by Salary===========================”);

Collections.sort(list,new SalaryComparator()); //passing salarycomparator object

Iterator<Employee> itr1=list.iterator();

while(itr1.hasNext())

{

System.out.println(itr1.next());

}

}

}

 

Output:

Sort by name=============================

[107,ashok,11500.99]

[108,rahul,13501.09]

[102,shaswot,11500.89]

[100,subham,12500.89]

Sort by Salary===========================

[102,shaswot,11500.89]

[107,ashok,11500.99]

[100,subham,12500.89]

[108,rahul,13501.09]

 

 

  • Here you can see when I am passing NameComparator it is sorting by name and same in SalaryComparator also.

If You guys have any doubt on this topic then feel free to comment and ask questions.

Leave a comment