Java:List集合内的对象进行排序

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()该怎么实现呢?

 public static void main(String[] args) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS");
Date now = new Date();
List<TaskAutoExecutePlan> items = new ArrayList<>(); try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
} TaskAutoExecutePlan plan = new TaskAutoExecutePlan();
plan.setId(UUID.randomUUID().toString());
plan.setCreateDate(new Date());
plan.setAutoExecutePlanType(AutoExecutePlanType.ImportSiteCellParameter);
items.add(plan); plan = new TaskAutoExecutePlan();
plan.setId(UUID.randomUUID().toString());
plan.setCreateDate(now);
plan.setAutoExecutePlanType(AutoExecutePlanType.ImportSiteCellParameter);
items.add(plan); plan = new TaskAutoExecutePlan();
plan.setId(UUID.randomUUID().toString());
plan.setCreateDate(now);
plan.setAutoExecutePlanType(AutoExecutePlanType.WIFI);
items.add(plan); plan = new TaskAutoExecutePlan();
plan.setId(UUID.randomUUID().toString());
plan.setCreateDate(new Date());
plan.setAutoExecutePlanType(AutoExecutePlanType.WIFI);
items.add(plan); System.out.println("before sort:");
for (TaskAutoExecutePlan item : items) {
System.out.println(item.getId() + ":" + item.getAutoExecutePlanType() + ":" + format.format(item.getCreateDate()));
} Collections.sort(items, new Comparator<TaskAutoExecutePlan>() {
@Override
public int compare(TaskAutoExecutePlan o1, TaskAutoExecutePlan o2) {
int i = o1.getAutoExecutePlanType().getValue() - o2.getAutoExecutePlanType().getValue();
if (i == 0) {
if (o1.getCreateDate().equals(o2.getCreateDate())) {
return 0;
} else if (o1.getCreateDate().after(o2.getCreateDate())) {
return 1;
} else {
return -1;
}
}
return i;
}
}); System.out.println("after sort:");
for (TaskAutoExecutePlan item : items) {
System.out.println(item.getId() + ":" + item.getAutoExecutePlanType() + ":" + format.format(item.getCreateDate()));
}
}

输出结果:

before sort:
252409be-1bb2-4502-8796-167b43d8de92:ImportSiteCellParameter:2017-02-27 19:36:11.000320
fd7dc92b-b0f4-423a-8644-d95ea0bc6cab:ImportSiteCellParameter:2017-02-27 19:36:00.000294
42bc8d28-90c4-4563-83a3-215c38bc0f9e:WIFI:2017-02-27 19:36:00.000294
d25d4201-f776-4b5f-bf33-8b1f8b80c412:WIFI:2017-02-27 19:36:11.000327
after sort:
fd7dc92b-b0f4-423a-8644-d95ea0bc6cab:ImportSiteCellParameter:2017-02-27 19:36:00.000294
252409be-1bb2-4502-8796-167b43d8de92:ImportSiteCellParameter:2017-02-27 19:36:11.000320
42bc8d28-90c4-4563-83a3-215c38bc0f9e:WIFI:2017-02-27 19:36:00.000294
d25d4201-f776-4b5f-bf33-8b1f8b80c412:WIFI:2017-02-27 19:36:11.000327

上一篇:我的wifi


下一篇:ALTERA MAX10官方评估板,新鲜出炉!