MyDate dd1 = new MyDate(2001, 3, 1);
MyDate dd2 = new MyDate(2001, 2, 4);
MyDate dd3 = new MyDate(2001, 1, 2);
MyDate dd4 = new MyDate(2001, 4, 5);
MyDate dd5 = new MyDate(2001, 2, 1);
MyDate dd6 = new MyDate(2001, 1, 1);
List l = new ArrayList();
l.add(dd1);
l.add(dd2);
l.add(dd3);
l.add(dd4);
l.add(dd5);
l.add(dd6);
Collections.sort(l, new Comp2()); //Collections.sort(I);
System.out.println(l);
import java.util.*;
public class Comp2 implements Comparator {
public int compare(Object o1, Object o2) {
if (!(o1 instanceof MyDate)) return -1;
if (!(o2 instanceof MyDate)) return -1;
if (o1 == o2) return 0;
final MyDate myDate1 = (MyDate) o1;
final MyDate myDate2 = (MyDate) o2;
if (myDate1.getMonth() > myDate2.getMonth()) return 1;
if (myDate1.getMonth() < myDate2.getMonth()) return -1;
if (myDate1.getYear() > myDate2.getYear()) return 1;
if (myDate1.getYear() < myDate2.getYear()) return -1;
if (myDate1.getDay() > myDate2.getDay()) return 1;
if (myDate1.getDay() < myDate2.getDay()) return -1;
return 0;
}
}