public class DateSort{
public static void main(String[] args){
Date[] days = new Date[5];
days[0] = new Date(2001,5,1);
days[1] = new Date(2001,4,1);
days[2] = new Date(2002,10,1);
days[3] = new Date(2002,1,15);
days[4] = new Date(2008,8,8);
System.out.println(" :");
for(int i=0;i < days.length;i++){
System.out.println(days[i]);
}
sort(days);
System.out.println(" :");
for(int i=0;i < days.length;i++){
System.out.println(days[i]);
}
}
public static Date[] sort(Date[] d){
int len = d.length;
for(int i=0;i<len;i++){
for(int j = i+1;j<len;j++){
if(d[i].compareTo(d[j]) > 0){
Date temp = d[i];
d[i] = d[j];
d[j] = temp;
}
}
}
return d;
}
}
class Date{
int year,month,day;
public Date(int year,int month,int day){
this.year = year;
this.month = month;
this.day = day;
}
public int compareTo(Date antherDate){
return year > antherDate.year?1
:year < antherDate.year?-1
:month > antherDate.month?1
:month < antherDate.month?-1
:day > antherDate.day?1
:day<antherDate.day?-1:0;
}
public String toString(){
return year+"/"+month+"/"+day;
}
}