Rajinder Menu

Comparable and Comparator -Part 3



Using Arrays.sort() and Collections.sort() with Comparable

In EmployeeComparision class in part 2 you have compared two employees using 'if 'clause as:

if(emp1.compareTo(emp2) < 0)

This is good for comparing only two employees but in real situations there may be many employees and you may have an array or a list containing them. Then you can use sort() method of Arrays class as:

EmployeeComparision.java
import java.util.Arrays;

public class EmployeeComparision{
 
    public static void main(String args[]){
     
        Employee emp1 = new Employee();      
        emp1.setEmpId(3);
        emp1.setName("Mr. A");
        emp1.setCity("NY");
        emp1.setSalary(25000.0);
      
        Employee emp2 = new Employee();      
        emp2.setEmpId(2);
        emp2.setName("Mr. B");
        emp2.setCity("Los Angeles");
        emp2.setSalary(35000.0);
     
        Employee emp3 = new Employee();      
        emp3.setEmpId(1);
        emp3.setName("Mr. C");
        emp3.setCity("Chicago");
        emp3.setSalary(35000.0);
      
        Employee emp4 = new Employee();      
        emp4.setEmpId(4);
        emp4.setName("Mr. D");
        emp4.setCity("Los Angeles");
        emp4.setSalary(35000.0);
      
        Employee emp5 = new Employee();      
        emp5.setEmpId(5);
        emp5.setName("Mr. E");
        emp5.setCity("Boston");
        emp5.setSalary(35000.0);
      
        /* An array containing Employees */
        Employee [] empArray = new Employee[5];
        empArray[0]=emp1;
        empArray[1]=emp2;
        empArray[2]=emp3;
        empArray[3]=emp4;
        empArray[4]=emp5;
      
        System.out.println("Employees in acsending order(according to their ids):");
     
        Arrays.sort(empArray);

        for(int i=0;i<=4;i++){
        System.out.println(empArray[i].getName()+", ");
      
        }      
      
    }

}

Similarly you can use Collections.sort() in above code. Collections.sort() accepts List as argument instead of an array:


/* A List containing Employees */
  List<Employee> empList = new ArrayList<Employee>();
  empList.add(emp1);
  empList.add(emp2);
  empList.add(emp3);
  empList.add(emp4);
  empList.add(emp5);
  
 System.out.println("Employees in acsending order(according to their ids):");
     
 Collections.sort(empList);

 for(int i=0;i<empList.size();i++){
  System.out.println(empList.get(i).getName()+", ");
}


So up to now in part 1, part 2, part 3 and in part 4 you have been writing code to sort your Employee instances based on their ids. But suppose now you have a requirement of sorting employess in ascending order of their names also. What would you do? Would you change logic in compareTo() method for sorting employess according to their names? -- Nah!

And suppose sometimes later you again have a requirement of sorting your employees according to their salary. Would you again change compareTo() for this?  Again Nah!

What you need for this is Comparator interface.

Click on Next >> to read about Comparator.







I would like to know your comments and if you liked the article then please share it on social networking buttons.


2 comments:

  1. Collections.sort(empList) will throw ClassCastException.

    ReplyDelete
  2. Not the correct code

    ReplyDelete