对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 }
- public class ListSort {
- public static class UserBean {
- private String id;
- private String birthday;
- public String getId() {
- return id;
- }
- public void setId(String id) {
- this.id = id;
- }
- public String getBirthday() {
- return birthday;
- }
- public void setBirthday(String birthday) {
- this.birthday = birthday;
- }
- }
- public static void main(String[] args) {
- List<UserBean> list = new ArrayList<UserBean>();
- UserListGenerate(list);
- System.out.println("排序前:"+list);
- ListSort(list);
- System.out.println("排序后:"+list);
- }
- private static void UserListGenerate(List<UserBean> list) {
- UserBean user1 = new UserBean();
- UserBean user2 = new UserBean();
- UserBean user3 = new UserBean();
- user1.setId("zhagnsan");
- user1.setBirthday("1980-11-01");
- user2.setId("lisi");
- user2.setBirthday("1981-12-01");
- user3.setId("wangwu");
- user3.setBirthday("1980-12-01");
- list.add(user1);
- list.add(user2);
- list.add(user3);
- }
- private static void ListSort(List<UserBean> list) {
- Collections.sort(list, new Comparator<UserBean>() {
- @Override
- public int compare(UserBean o1, UserBean o2) {
- SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
- try {
- Date dt1 = format.parse(o1.getBirthday());
- Date dt2 = format.parse(o2.getBirthday());
- if (dt1.getTime() > dt2.getTime()) {
- return 1;
- } else if (dt1.getTime() < dt2.getTime()) {
- return -1;
- } else {
- return 0;
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return 0;
- }
- });
- }
- }
由于本人是自己学习总结出来的,有不足之处,请各位看官批评指出,我将及时改正,以提高知识总结的正确性和严谨性,为大家学习提供方便!!!
如若转载,请注明出处!!!