Monday, March 30, 2009

Java : Sorting User Objects using Comparable / Comparator in Ascending or Descending order

Comparable



java.lang.Comparable

  • int objOne.compareTo(objTwo)

  • Returns
    negative if objOne < objTwo
    zero if objOne == objTwo
    positive if objOne > objTwo

  • You must modify the class whose
    instances you want to sort.

  • Only one sort sequence can be created

  • Implemented frequently in the API by:
    String, Wrapper classes, Date, Calendar...



//Complete code for SortingObject.java using Comparable


import java.util.*;

class Employee implements Comparable<Employee>//using Comparable
{
private Integer empId;
private String name;

Employee(Integer empId,String name)
{
this.empId=empId;
this.name=name;
}

public String toString()
{
return "ID: "+empId+" Name: "+name+"\n";
}

public int compareTo(Employee e)
{

return empId.compareTo(e.empId);//Sorting in Ascending order of empId
//return e.empId.compareTo(empId);//Sorting in Descending order of empId
//return name.compareTo(e.name);//Sorting in Ascending order of name
//return e.name.compareTo(name);//Sorting in Descending order of name


}
}

public class SortingObject
{
public static void main(String[] args)
{
ArrayList<Employee> employeeArray=new ArrayList<Employee>();

employeeArray.add(new Employee(11,"Ram"));
employeeArray.add(new Employee(44,"Amol"));
employeeArray.add(new Employee(9,"Kanchan"));
employeeArray.add(new Employee(22,"Madan"));
employeeArray.add(new Employee(13,"Ashwini"));

System.out.println("Before Sorting.....");
System.out.println(employeeArray);

Collections.sort(employeeArray);

System.out.println("After Sorting.....");
System.out.println(employeeArray);
}
}//end



Comparator



java.util.Comparator

  • int compare(objOne, objTwo)

  • Returns
    negative if objOne < objTwo
    zero if objOne == objTwo
    positive if objOne > objTwo

  • You build a class separate from the class whose instances you
    want to sort.

  • Many sort sequences can be created

  • Meant to be implemented to sort instances of third-party
    classes.




//Complete code for SortingObject.java using Comparator


import java.util.*;

class Employee
{
private Integer empId;
private String name;

Employee(Integer empId,String name)
{
this.empId=empId;
this.name=name;
}

public String getName()
{
return name;
}

public Integer getEmpId()
{
return empId;
}

public String toString()
{
return "ID: "+empId+" Name: "+name+"\n";
}
}

class EmployeeComparator implements Comparator<Employee>//using Comparator
{
public int compare(Employee a,Employee b)
{
return a.getEmpId().compareTo(b.getEmpId());//Sorting in Ascending order of empId
//return b.getEmpId().compareTo(a.getEmpId());//Sorting in Descending order of empId
//return a.getName().compareTo(b.getName());//Sorting in Ascending order of name
//return b.getName().compareTo(a.getName());//Sorting in Descending order of name
}
}

public class SortingObject
{
public static void main(String[] args)
{
ArrayList<Employee> employeeArray=new ArrayList<Employee>();

employeeArray.add(new Employee(11,"Ram"));
employeeArray.add(new Employee(44,"Amol"));
employeeArray.add(new Employee(9,"Kanchan"));
employeeArray.add(new Employee(22,"Madan"));
employeeArray.add(new Employee(13,"Ashwini"));

System.out.println("Before Sorting.....");
System.out.println(employeeArray);

EmployeeComparator empComp = new EmployeeComparator();

Collections.sort(employeeArray,empComp);

System.out.println("After Sorting.....");
System.out.println(employeeArray);
}
}//end