我正在尝试对日期列表进行排序,但它不起作用。
下面是AttEnt中的声明和get函数
@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()
发布于 2017-04-29 04:23:11
Comparator<AttEnt> comparator = Comparator.comparing(AttEnt::getEndTime).reversed();
attempts.sort(comparator);接口中的Java静态方法是您的好朋友
点击HERE了解更多精彩
https://stackoverflow.com/questions/43688230
复制相似问题