There are times when you have a list of some user defined java beans that you need to be sorted with respect to a certain property of the javabean. This tutorial explains how the Comparator interface can be used to sort the List.
First lets create a Java bean:
public class ScheduleBean {
public String date;
public String openTime;
public String closeTime;
/**
* @return the closeTime
*/
public String getCloseTime() {
return closeTime;
}
/**
* @param closeTime the closeTime to set
*/
public void setCloseTime(String closeTime) {
this.closeTime = closeTime;
}
/**
* @return the date
*/
public String getDate() {
return date;
}
/**
* @param date the date to set
*/
public void setDate(String date) {
this.date = date;
}
/**
* @return the openTime
*/
public String getOpenTime() {
return openTime;
}
/**
* @param openTime the openTime to set
*/
public void setOpenTime(String openTime) {
this.openTime = openTime;
}
}
The above code creates a ScheduleBean class with Date, OpenTime and closeTime. Now if we had a List of ScheduleBean, and we wanted to compare the beans stored in the List and maybe sort them based on the Date, we would need a custom class that implements the Comparator interface. The Comparator interface has two methods that can be implemented. They are:
int compare(Object o1, Object o2)
boolean equals(Object obj)
The following is our MyComparator class:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Comparator;
import java.util.Date;
public class MyComparator implements Comparator {
public int compare(Object arg0, Object arg1) {
ScheduleBean s1 = (ScheduleBean)arg0;
ScheduleBean s2 = (ScheduleBean)arg1;
System.out.println("Comparing " + s1.getDate() + " " + s2.getDate());
if(compareDate(s1.getDate(),s2.getDate())) {
return 1;
} else {
return -1;
}
}
private boolean compareDate(String date1Str, String date2Str) {
SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd");
boolean difference = false;
Date date1, date2;
try {
date1 = sdfDate.parse(date1Str);
date2 = sdfDate.parse(date2Str);
if(date1.after(date2)){
difference = true;
}
} catch (ParseException e) {
e.printStackTrace();
}
return difference;
}
}
In the above code, we implement the compare method. In the compare method, we take 2 arguments and typecast them to the beans we will be comparing and then we use the compareDate method that we have written to return a positive 1 or a negative 1. Notice that the compare method should return a negative integer, zero, or a positive integer if the first argument is less than, equal to, or greater than the second.
Now we use the MyComparator class to sort the List of ScheduleBean
Comparator myComparator = new MyComparator();
Collections.sort(ScheduleList,myComparator);
Complete code here:
public class Tutorial {
/**
* @param args
*/
public static void main(String[] args) {
Tutorial tutorial = new Tutorial();
List scheduleList = new ArrayList();
ScheduleBean ssb = new ScheduleBean();
ssb.setDate("2009-03-30");
ssb.setOpenTime("00:00");
ssb.setCloseTime("00:00");
scheduleList.add(ssb);
ssb = new ScheduleBean();
ssb.setDate("2009-04-03");
ssb.setOpenTime("00:00");
ssb.setCloseTime("00:00");
scheduleList.add(ssb);
ssb = new ScheduleBean();
ssb.setDate("2009-04-08");
ssb.setOpenTime("00:00");
ssb.setCloseTime("00:00");
scheduleList.add(ssb);
ssb = new ScheduleBean();
ssb.setDate("2009-04-04");
ssb.setOpenTime("00:00");
ssb.setCloseTime("00:00");
scheduleList.add(ssb);
ssb = new ScheduleBean();
ssb.setDate("2009-04-05");
ssb.setOpenTime("00:00");
ssb.setCloseTime("00:00");
scheduleList.add(ssb);
ssb = new ScheduleBean();
ssb.setDate("2009-04-04");
ssb.setOpenTime("12:00");
ssb.setCloseTime("00:00");
scheduleList.add(ssb);
ssb = new ScheduleBean();
ssb.setDate("2009-04-06");
ssb.setOpenTime("00:00");
ssb.setCloseTime("01:00");
scheduleList.add(ssb);
System.out.println("Before Sorting");
Iterator scheduleItr = scheduleList.iterator();
while(scheduleItr.hasNext()) {
ScheduleBean storeSchedule = (ScheduleBean) scheduleItr.next();
System.out.println(storeSchedule.getDate() + " " +
storeSchedule.getOpenTime() + " " +
storeSchedule.getCloseTime());
}
System.out.println("======= NOW SORTING =======");
Comparator myComparator = new MyComparator();
Collections.sort(scheduleList,myComparator);
System.out.println("======= SORTED =======");
scheduleItr = scheduleList.iterator();
while(scheduleItr.hasNext()) {
ScheduleBean storeSchedule = (ScheduleBean) scheduleItr.next();
System.out.println(storeSchedule.getDate() + " " +
storeSchedule.getOpenTime() + " " +
storeSchedule.getCloseTime());
}
}
}
And, the result would be as shown below
Before Sorting
2009-03-30 00:00 00:00
2009-04-03 00:00 00:00
2009-04-08 00:00 00:00
2009-04-04 00:00 00:00
2009-04-05 00:00 00:00
2009-04-04 12:00 00:00
2009-04-06 00:00 01:00
======= NOW SORTING =======
Comparing 2009-03-30 2009-04-03
Comparing 2009-04-03 2009-04-08
Comparing 2009-04-04 2009-04-05
Comparing 2009-04-05 2009-04-04
Comparing 2009-04-04 2009-04-04
Comparing 2009-04-05 2009-04-06
Comparing 2009-04-08 2009-04-04
Comparing 2009-03-30 2009-04-04
Comparing 2009-04-03 2009-04-04
Comparing 2009-04-08 2009-04-04
Comparing 2009-04-08 2009-04-04
Comparing 2009-04-08 2009-04-05
Comparing 2009-04-08 2009-04-06
======= SORTED =======
2009-03-30 00:00 00:00
2009-04-03 00:00 00:00
2009-04-04 00:00 00:00
2009-04-04 12:00 00:00
2009-04-05 00:00 00:00
2009-04-06 00:00 01:00
2009-04-08 00:00 00:00