Thursday, September 15, 2011

How to use Comparator and Comparable to sort the objects?


Today, we will see how can we sort the elements of an Array or Collections. There are two utilities to sort the collection of objects -

  • Comparable - This interface contains only one method prototype i.e. compareTo (Object obj). This method returns a negative integer, zero, or a positive integer as this object (through which the method has been called) is less than, equal to, or greater than the specified object. To make our objects comparable we should implement our class to this interface.


Here is the sample code -

public class Employee implements Comparable {
    int id;
    String name;
    Date dob;

    Employee(int id, String name, Date date) {
        this.id = id;
        this.name  = name;
        this.dob  = dob;
    }
    
    //If you want to sort the Employee Collection based on empId;
    public int compareTo(Object object) {
        Employee emp = (Employee) object;
        // If you want the reversed order just reverse the subtraction as emp.id - this.id
        return this.id - emp.id;
    }

    //If you want to sort the Employee Collection based on name;
    public int compareTo(Object object) {
        Employee emp = (Employee) object;
        // If you want the reversed order call the below compare method using emp.name and pass this.name
        return this.name.compareTo(emp.name);
    }

    //If you want to sort the Employee Collection based on DOB;
    public int compareTo(Object object) {
        Employee emp = (Employee) object;
        // If you want the reversed order just reverse the subtraction
        return this.dob.equals(emp.dob);
    }
}

public class EmployeeSorter {
    public static void main(String[] args) throws Exception {
        DateFormat formatter = new SimpleDateFormat("dd-mm-yyyy");
        Employee emp1 = new Employee(1, "Ram", formatter.parse("26-01-1988");
        Employee emp1 = new Employee(3, "Shyam", formatter.parse("14-10-1984");
        Employee emp1 = new Employee(2, "Ghanshyam", formatter.parse("25-01-1983");
        List listOfEmployee = new ArrayList();
        listOfEmployee.add(emp1);
        listOfEmployee.add(emp2);
        listOfEmployee.add(emp3);
        Collections.sort(listOfEmployee);
    }
}        


  • Comparator - This interface contains two methods prototype. One is compare(Object obj1, Object obj2) and equals(Object obj). To sort the elements, we just have to override the compare method. This method also returns a negative integer, zero, or a positive integer as this object (through which the method has been called) is less than, equal to, or greater than the specified object. To make the objects comparable we have to create a class implementing this interface and override the compare method.


Here is the code -

public class Employee {
    int id;
    String name;
    Date dob;

    Employee(int id, String name, Date date) {
        this.id = id;
        this.name  = name;
        this.dob  = dob;
    }
    
    //If you want to sort the Employee Collection based on empId;
    public int compareTo(Object object) {
        Employee emp = (Employee) object;
        // If you want the reversed order just reverse the subtraction as emp.id - this.id
        return this.id - emp.id;
    }

    //If you want to sort the Employee Collection based on name;
    public int compareTo(Object object) {
        Employee emp = (Employee) object;
        // If you want the reversed order call the below compare method using emp.name and pass this.name
        return this.name.compareTo(emp.name);
    }

    //If you want to sort the Employee Collection based on DOB;
    public int compareTo(Object object) {
        Employee emp = (Employee) object;
        // If you want the reversed order just reverse the subtraction
        return this.dob.eqauls(emp.dob);
    }
}

public class EmployeeComparator implements Comparator {
    //If you want to sort the Employee Collection based on empId;
    public int compareTo(Object obj1, Object obj2) {
        Employee emp1 = (Employee) obj1;
        Employee emp2 = (Employee) obj2;
        // If you want the reversed order just reverse the subtraction as emp.id - this.id
        return emp1.id - emp2.id;
    }

    //If you want to sort the Employee Collection based on name;
    public int compareTo(Object obj1, Object obj2) {
        Employee emp1 = (Employee) obj1;
        Employee emp2 = (Employee) obj2;
        // If you want the reversed order call the below compare method using emp.name and pass this.name
        return emp1.name.compareTo(emp2.name);
    }

    //If you want to sort the Employee Collection based on DOB;
    public int compareTo(Object obj1, Object obj2) {
        Employee emp1 = (Employee) obj1;
        Employee emp2 = (Employee) obj2;
        // If you want the reversed order just reverse the subtraction
        return emp1.dob.equals(emp2.dob);
    }
}

public class EmployeeSorter {
    public static void main(String[] args) throws Exception {
        DateFormat formatter = new SimpleDateFormat("dd-mm-yyyy");
        Employee emp1 = new Employee(1, "Ram", formatter.parse("26-01-1988");
        Employee emp1 = new Employee(3, "Shyam", formatter.parse("14-10-1984");
        Employee emp1 = new Employee(2, "Ghanshyam", formatter.parse("25-01-1983");
        List listOfEmployee = new ArrayList();
        listOfEmployee.add(emp1);
        listOfEmployee.add(emp2);
        listOfEmployee.add(emp3);
        Collections.sort(listOfEmployee, new EmployeeComparator());
    }
}

Note: The difference between two approach is in case of Comparable, we have to create our class whose objects need to be sorted and implement Comparable interface with overriding compare method which takes only one argument. While in case of Comparator, we have to create our own Comparator and implement Comparator interface with overriding compareTo mehod which takes two arguments.

1 comment:

  1. Thank you a lot for giving everyone an extraordinarily nice opportunity to read from this blog.

    ReplyDelete