List集合中的对象进行排序,除了for外,还有java的Collections对象来对摸个集合进行排序的用法。
比如说我有一个List集合,集合元素为:
public class TaskAutoExecutePlan{ private String id = null; private AutoExecutePlanType autoExecutePlanType; private TaskStatus taskStatus; private String parameters; private Date createDate; private Date completeDate; private Date modifyDate; /**get set 方法*/}public enum AutoExecutePlanType { ImportSiteCellParameter(2), WIFI(32); /*构造函数等*/}
如果我需要先按照AutoExecutePlanType进行升序排序,在对CreateDate进行升序排序,使用Collections.sort()该怎么实现呢?
1 public static void main(String[] args) { 2 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS"); 3 Date now = new Date(); 4 Listitems = new ArrayList<>(); 5 6 try { 7 Thread.sleep(10000); 8 } catch (InterruptedException e) { 9 e.printStackTrace();10 }11 12 TaskAutoExecutePlan plan = new TaskAutoExecutePlan();13 plan.setId(UUID.randomUUID().toString());14 plan.setCreateDate(new Date());15 plan.setAutoExecutePlanType(AutoExecutePlanType.ImportSiteCellParameter);16 items.add(plan);17 18 19 plan = new TaskAutoExecutePlan();20 plan.setId(UUID.randomUUID().toString());21 plan.setCreateDate(now);22 plan.setAutoExecutePlanType(AutoExecutePlanType.ImportSiteCellParameter);23 items.add(plan);24 25 plan = new TaskAutoExecutePlan();26 plan.setId(UUID.randomUUID().toString());27 plan.setCreateDate(now);28 plan.setAutoExecutePlanType(AutoExecutePlanType.WIFI);29 items.add(plan);30 31 plan = new TaskAutoExecutePlan();32 plan.setId(UUID.randomUUID().toString());33 plan.setCreateDate(new Date());34 plan.setAutoExecutePlanType(AutoExecutePlanType.WIFI);35 items.add(plan);36 37 System.out.println("before sort:");38 for (TaskAutoExecutePlan item : items) {39 System.out.println(item.getId() + ":" + item.getAutoExecutePlanType() + ":" + format.format(item.getCreateDate()));40 }41 42 Collections.sort(items, new Comparator () {43 @Override44 public int compare(TaskAutoExecutePlan o1, TaskAutoExecutePlan o2) {45 int i = o1.getAutoExecutePlanType().getValue() - o2.getAutoExecutePlanType().getValue();46 if (i == 0) {47 if (o1.getCreateDate().equals(o2.getCreateDate())) {48 return 0;49 } else if (o1.getCreateDate().after(o2.getCreateDate())) {50 return 1;51 } else {52 return -1;53 }54 }55 return i;56 }57 });58 59 System.out.println("after sort:");60 for (TaskAutoExecutePlan item : items) {61 System.out.println(item.getId() + ":" + item.getAutoExecutePlanType() + ":" + format.format(item.getCreateDate()));62 }63 }
输出结果:
before sort:
252409be-1bb2-4502-8796-167b43d8de92:ImportSiteCellParameter:2017-02-27 19:36:11.000320fd7dc92b-b0f4-423a-8644-d95ea0bc6cab:ImportSiteCellParameter:2017-02-27 19:36:00.00029442bc8d28-90c4-4563-83a3-215c38bc0f9e:WIFI:2017-02-27 19:36:00.000294d25d4201-f776-4b5f-bf33-8b1f8b80c412:WIFI:2017-02-27 19:36:11.000327after sort:fd7dc92b-b0f4-423a-8644-d95ea0bc6cab:ImportSiteCellParameter:2017-02-27 19:36:00.000294252409be-1bb2-4502-8796-167b43d8de92:ImportSiteCellParameter:2017-02-27 19:36:11.00032042bc8d28-90c4-4563-83a3-215c38bc0f9e:WIFI:2017-02-27 19:36:00.000294d25d4201-f776-4b5f-bf33-8b1f8b80c412:WIFI:2017-02-27 19:36:11.000327