When all the time we want to sort the list of object on the basis of only one of the property of object, we should use Comparable interface. On the other hand there may be need to sort the list of object on the basis of different different property at different different time, we should go with Comparator interface. We create a separate Comparator implementation class for each property on the basis of which we are going to sort the list, and we pass the object of this Comparator class in sort method of Collections as a second parameter. Let´s see below example:
Comparable Interface Example:
---------------------------------------------------------------------
Payment.java:
-------------------------------------------
package in.anyforum;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Payment implements Comparable<Payment>{
private int id;
private double amount;
@Override
public int compareTo(Payment p) {
// TODO Auto-generated method stub
if(this.getAmount()>p.getAmount())
return 1;
else if(this.getAmount()<p.getAmount())
return -1;
else
return 0;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
public static void main(String[] args) {
List<Payment> payment= new ArrayList<Payment>();
Payment p1=new Payment();
p1.setId(111);
p1.setAmount(10000);
Payment p2=new Payment();
p2.setId(122);
p2.setAmount(9000);
Payment p3=new Payment();
p3.setId(133);
p3.setAmount(19000);
payment.add(p1);
payment.add(p2);
payment.add(p3);
Collections.sort(payment);
System.out.println("Sorted Payment by Comparable:");
for(Payment p:payment){
System.out.println("ID="+p.getId()+" Amount="+p.getAmount());
}
}
}
Output:
-------------------------------
Sorted Payment by Comparable:
ID=122 Amount=9000.0
ID=111 Amount=10000.0
ID=133 Amount=19000.0
Comparator Interface Example:
---------------------------------------------------------------------
Employee.java:
-----------------------------------------
package in.anyforum;
public class Employee {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
IdComparator.java:
-------------------------------------------------------
package in.anyforum;
import java.util.Comparator;
public class IdComparator implements Comparator<Employee>{
@Override
public int compare(Employee e1, Employee e2) {
if(e1.getId()==e2.getId())
return 0;
else if(e1.getId()>e2.getId())
return 1;
else
return -1;
}
}
NameComparator.java:
-----------------------------------------------------
package in.anyforum;
import java.util.Comparator;
public class NameComparator implements Comparator<Employee>{
@Override
public int compare(Employee e1, Employee e2) {
/*In this case we are using the method compareTo() of String class*/
return e1.getName().compareTo(e2.getName());
}
}
SortComparator.java:
-----------------------------------------------------
package in.anyforum;
import java.util.ArrayList;
import java.util.Collections;
public class SortComparator {
public static void main(String s1[]){
ArrayList<Employee> list=new ArrayList<Employee>();
Employee e1=new Employee();
e1.setId(321);
e1.setName("ABC");
Employee e2=new Employee();
e2.setId(221);
e2.setName("MBC");
Employee e3=new Employee();
e3.setId(111);
e3.setName("JCB");
list.add(e1);
list.add(e2);
list.add(e3);
Collections.sort(list, new IdComparator());
System.out.println("After sorting list of Employees based on id is as follows.....");
for(Employee e:list){
System.out.println("id="+e.getId()+" and name="+e.getName());
}
Collections.sort(list, new NameComparator());
System.out.println("After sorting list of Employees based on name is as follows.....");
for(Employee e:list){
System.out.println("id="+e.getId()+" and name="+e.getName());
}
}
}
Output:
----------------------------------------
After sorting list of Employees based on id is as follows.....
id=111 and name=JCB
id=221 and name=MBC
id=321 and name=ABC
After sorting list of Employees based on name is as follows.....
id=321 and name=ABC
id=111 and name=JCB
id=221 and name=MBC
Note:
-----------------------------
if you want to reverse the order of sorting just reverse the order of object in overridden method of interface(Comparable/Comparator) like this: return e2.getName().compareTo(e1.getName());
5