Java Collection排序

时间:2017-04-28 20:14:06

标签: java sorting collections

我正在尝试对日期列表进行排序,但它无法正常工作。

这是AttEnt中的声明和获取功能

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "end_time")
private Date endTime;

public Date getEndTime() {
    return endTime;
}

以下是没有做任何事情的排序代码。 GetAttempts()获取所有被调用尝试的列表。它们不是有序的,我只是希望能够得到最新的endTime。

            List<AttEnt> attempts = called.getAttempts();
            Collections.sort(attempts, new Comparator<AttEnt>() {
            @Override
            public int compare(AttEnt a1, AttEnt a2) {
                if (a1.getEndTime() == null || a2.getEndTime() == null)
                    return 0;
                return a1.getEndTime().compareTo(a2.getEndTime());
                }
            });

我相信上面的代码应该排序尝试,然后在尝试之后应该排序,所以最新的结束时间是attempts.get(attempts.size() - 1).getEndTime()

1 个答案:

答案 0 :(得分:1)

Comparator<AttEnt> comparator = Comparator.comparing(AttEnt::getEndTime).reversed();
attempts.sort(comparator);

接口中的Java静态方法是你的朋友

点击HERE以获得更多精彩内容

相关问题