public class DateSort{
public static void main(String[] args){
Date[] days = new Date[4];
days[0]=new Date(1999,3,22);
days[1]=new Date(1989,3,12);
days[2]=new Date(1999,8,22);
days[3]=new Date(1909,3,2);
sort(days);
for(int i=0;i<days.length;i++){
System.out.println(days[i]);
}
}
public static Date[] sort(Date[] a){
for(int i=a.length-1;i>=1;i--){
for(int j=0;j<=i-1;j++){
if(a[j].compare(a[j+1])>0){
Date temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
return a;
}
}
class Date{
int year,day,month;
Date(int y,int m,int d ){
this.year=y; this.month=m;this.day=d;
}
public int compare(Date date){
return year>date.year ? 1
: year<date.year ? -1
: month>date.month ? 1
: month<date.month ? -1
: day>date.day ? 1
: day<date.day ? -1:0;
}
public String toString() {
return "Year:Month:Day--" + year"-" + month "-" + day;
}
}
------解决方案--------------------
+ year"-" + month "-"
这两处算怎么回事?!
------解决方案--------------------
应该改成这样:"Year:Month:Day--" + year+"-" + month +"-" + day;