Object sorting using Comparator and Comparable Interface in Java

 

Today we will discuss about how to sort Object using Comparator and Comparable interface in Java. 

       How to sort Object using Comparator and Comparable Interface in Java is a frequent question in most of the Core Java interview questions. It can be just theory question or even some interviewer may ask to write sample code for both. In that case one has to be prepared for both to explain the different and importance of both Interface in Java and also to write a sample code. 

       Lets see some important different between these two Interface's in Java. 

  • Comparator Interface is from java.util package and Comparable Interface is from java.lang package.
  • For using Comparator Interface we need to import java.util.Comparator, where as Comparable is by default. 
  • If we implement Comparator Interface then we need to implement method called public int compare(Object obj1, Object obj2) and for Comparable Interface we need to implement method called public int compareTo(Objectobj)
  • If we see Comparator Interface it will be used to Compare 2 objects different Objects of a class and where Comparable Interface will be used to Compare same class local member variables with the Object provided to CompareTo method. 

          
     Example:

       Below example Employee Class which will implement both Comparator and Comparable Interface and also has the method implementation for compare() and compareTo() methods. 
       compare() method has 2 input parameters of Employee class object and used to sort based on Employee Name (empName). 
       comapreTo() method has single Employee class object and sorted based Employee Address (empAddress)     


Employee.java

public class Employee implements Comparator<Employee>, Comparable<Employee> {

 private String empName;
 private String empAddress;

 public Employee() {
 }

 public Employee(String empName, String empAddress) {
  this.empAddress = empAddress;
  this.empName = empName;
 }

 public String getEmpName() {
  return empName;
 }

 public void setEmpName(String empName) {
  this.empName = empName;
 }

 public String getEmpAddress() {
  return empAddress;
 }

 public void setEmpAddress(String empAddress) {
  this.empAddress = empAddress;
 }

 public int compare(Employee o1, Employee o2) {
  String o1Name = o1.getEmpName();
  String o2Name = o2.getEmpName();
  if (o1Name.compareTo(o2Name) > 0) {
   return 1;
  } else if (o1Name.compareTo(o2Name) < 0) {
   return -1;
  } else {
   return 0;
  }
 }

 public int compareTo(Employee obj) {
  return this.empAddress.compareTo(obj.empAddress);
 }
}

       

CompratorComparableTest.java

public class CompratorComparableTest {
 public static void main(String[] args) {
  
  List<Employee> arr = new ArrayList<Employee>();
  arr.add(new Employee("jobs", "delhi"));
  arr.add(new Employee("mike", "bangalore"));
  arr.add(new Employee("vera", "madras"));
  arr.add(new Employee("raja", "chennai"));
  arr.add(new Employee("mani", "rorkee"));
  arr.add(new Employee("khan", "mumbai"));
  
  // Object sort using comprator
  Collections.sort(arr, new Employee());  
  Iterator<Employee> itr = arr.iterator();
  System.out.println("Object sort using comparator - sort by Name");
  while (itr.hasNext()) {
   Employee emp = (Employee) itr.next();
   System.out.println(emp.getEmpName()+" \t\t "+emp.getEmpAddress());
  }
  
  System.out.println("\n\n");
  
  //Object sort using comprable
  Collections.sort(arr);
  Iterator<Employee> itr1 = arr.iterator();
  System.out.println("Object sort using comprable - sort by Address");
  while (itr1.hasNext()) {
   Employee empobj = (Employee) itr1.next();
   System.out.println(empobj.getEmpName()+" \t\t "+empobj.getEmpAddress());
  }
 }
}




OUTPUT:


Object sort using comparator - sort by Name
jobs    delhi
khan    mumbai
mani    rorkee
mike    bangalore
raja    chennai
vera    madras



Object sort using comprable - sort by Address
mike    bangalore
raja    chennai
jobs    delhi
vera    madras
khan    mumbai
mani    rorkee


Hope you are clear on using Comparator and Comparable Interface in Java. Please drop your comments.  




 

1 comment:
Write comments