1 public class Test {
 2     public static void main(String args[]) {
 3         Date[] days = new Date[5];
 4         days[0] = new Date(2008,4,6);
 5         days[1] = new Date(2005,5,6);
 6         days[2] = new Date(2003,6,6);
 7         days[3] = new Date(2007,7,6);
 8         days[4] = new Date(2008,8,6);
 9         
10         bubbleSort(days);
11         
12         for(int i = 0;i < days.length; i++) {
13             System.out.println(days[i]);
14         }
15     }
16     
17     public static Date[] bubbleSort(Date[] a) {
18         int len = a.length;
19         for(int i =len-1; i >=1; i--) {
20             for(int j = 0; j <= i-1; j++) {
21                 if(a[j].compare(a[j+1]) > 0) {
22                     Date temp = a[j];
23                     a[j] = a[j+1];
24                     a[j+1] = temp;
25                 }
26             }
27         }
28         return a;
29     }
30 }
31 
32 class Date {
33     int year; int month; int day;
34     
35     Date(int y, int m, int d) {
36         year = y; month = m; day = d;
37     }
38     
39     public int compare(Date date) {
40         return year > date.year ? 1 
41                 : year < date.year ? - 1
42                 : month > date.month ? 1
43                 : month < date.month ? -1
44                 : day > date.day ? 1
45                 : day < date.day ? -1 : 0;        
46     }
47     
48     public String toString() {
49         return year + "-" + month + "-" + day;
50     }
51     
52 }

 

posted on 2018-08-24 10:55  蕾拉  阅读(144)  评论(0编辑  收藏  举报