对List中每个对象元素按时间顺序排序

 1 import java.util.*;  
 2   
 3 public class ListSort {  
 4     public static class UserBean {  
 5         private String id;  
 6         private String birthday;  
 7   
 8         public String getId() {  
 9             return id;  
10         }  
11   
12         public void setId(String id) {  
13             this.id = id;  
14         }  
15   
16         public String getBirthday() {  
17             return birthday;  
18         }  
19   
20         public void setBirthday(String birthday) {  
21             this.birthday = birthday;  
22         }  
23     }  
24   
25     public static void main(String[] args) {  
26         List<UserBean> list = new ArrayList<UserBean>();  
27         UserListGenerate(list);  
28         System.out.println("排序前:"+list);  
29   
30         ListSort(list);  
31         System.out.println("排序后:"+list);  
32     }  
33   
34     private static void UserListGenerate(List<UserBean> list) {  
35         UserBean user1 = new UserBean();  
36         UserBean user2 = new UserBean();  
37         UserBean user3 = new UserBean();  
38         user1.setId("zhagnsan");  
39         user1.setBirthday("1980-11-01");  
40   
41         user2.setId("lisi");  
42         user2.setBirthday("1981-12-01");  
43   
44         user3.setId("wangwu");  
45         user3.setBirthday("1980-12-01");  
46   
47         list.add(user1);  
48         list.add(user2);  
49         list.add(user3);  
50     }  
51   
52     private static void ListSort(List<UserBean> list) {  
53         Collections.sort(list, new Comparator<UserBean>() {  
54             @Override  
55             public int compare(UserBean o1, UserBean o2) {  
56                 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
57                 try {  
58                     Date dt1 = format.parse(o1.getBirthday());  
59                     Date dt2 = format.parse(o2.getBirthday());  
60                     if (dt1.getTime() > dt2.getTime()) {  
61                         return 1;  
62                     } else if (dt1.getTime() < dt2.getTime()) {  
63                         return -1;  
64                     } else {  
65                         return 0;  
66                     }  
67                 } catch (Exception e) {  
68                     e.printStackTrace();  
69                 }  
70                 return 0;  
71             }  
72         });  
73     }  
74 }

 

  1. public class ListSort {  
  2.     public static class UserBean {  
  3.         private String id;  
  4.         private String birthday;  
  5.   
  6.         public String getId() {  
  7.             return id;  
  8.         }  
  9.   
  10.         public void setId(String id) {  
  11.             this.id = id;  
  12.         }  
  13.   
  14.         public String getBirthday() {  
  15.             return birthday;  
  16.         }  
  17.   
  18.         public void setBirthday(String birthday) {  
  19.             this.birthday = birthday;  
  20.         }  
  21.     }  
  22.   
  23.     public static void main(String[] args) {  
  24.         List<UserBean> list = new ArrayList<UserBean>();  
  25.         UserListGenerate(list);  
  26.         System.out.println("排序前:"+list);  
  27.   
  28.         ListSort(list);  
  29.         System.out.println("排序后:"+list);  
  30.     }  
  31.   
  32.     private static void UserListGenerate(List<UserBean> list) {  
  33.         UserBean user1 = new UserBean();  
  34.         UserBean user2 = new UserBean();  
  35.         UserBean user3 = new UserBean();  
  36.         user1.setId("zhagnsan");  
  37.         user1.setBirthday("1980-11-01");  
  38.   
  39.         user2.setId("lisi");  
  40.         user2.setBirthday("1981-12-01");  
  41.   
  42.         user3.setId("wangwu");  
  43.         user3.setBirthday("1980-12-01");  
  44.   
  45.         list.add(user1);  
  46.         list.add(user2);  
  47.         list.add(user3);  
  48.     }  
  49.   
  50.     private static void ListSort(List<UserBean> list) {  
  51.         Collections.sort(list, new Comparator<UserBean>() {  
  52.             @Override  
  53.             public int compare(UserBean o1, UserBean o2) {  
  54.                 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
  55.                 try {  
  56.                     Date dt1 = format.parse(o1.getBirthday());  
  57.                     Date dt2 = format.parse(o2.getBirthday());  
  58.                     if (dt1.getTime() > dt2.getTime()) {  
  59.                         return 1;  
  60.                     } else if (dt1.getTime() < dt2.getTime()) {  
  61.                         return -1;  
  62.                     } else {  
  63.                         return 0;  
  64.                     }  
  65.                 } catch (Exception e) {  
  66.                     e.printStackTrace();  
  67.                 }  
  68.                 return 0;  
  69.             }  
  70.         });  
  71.     }  
  72. }
posted @ 2017-12-13 16:00  硝烟漫过十八岁  阅读(4204)  评论(0编辑  收藏  举报