【发布时间】:2017-09-28 23:46:51
【问题描述】:
我正在尝试对日期列表进行排序,但它不起作用。
这是AttemptEntity中的声明和get函数
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "end_time")
private Date endTime;
public Date getEndTime() {
return endTime;
}
这是没有做任何事情的排序代码。 GetAttempts() 获取所有被调用尝试的列表。它们不按顺序排列,我只想能够获得具有最新 endTime 的任何尝试。
List<AttemptEntity> attempts = called.getAttempts();
Collections.sort(attempts, new Comparator<AttemptEntity>() {
@Override
public int compare(AttemptEntity a1, AttemptEntity a2) {
if (a1.getEndTime() == null || a2.getEndTime() == null)
return 0;
return a1.getEndTime().compareTo(a2.getEndTime());
}
});
我相信上面的代码应该对尝试进行排序,然后在尝试之后进行排序,所以最后的结束时间应该是尝试.get(attempts.size()-1).getEndTime()
我尝试使用以下内容,但也根本没有进行任何排序。输入列表和输出列表完全相同。
Comparator<AttemptEntity> comparator = Comparator.comparing(AttemptEntity::getEndTime).reversed();
attempts.sort(comparator);
【问题讨论】:
-
如果有没有
endTime的尝试,您的compare方法会认为它等于每个 其他实例。我不确定这样的通配符会对二进制排序过程产生什么影响。
标签: java sorting date collections comparator