[英]Sort ArrayList w.r.t Date in java
I am trying to sort ArrayList wrt termDate ArrayList contains firstName,lastName,email,startDate,termDate 我正在尝试对termList的ArrayList进行排序ArrayList包含firstName,lastName,email,startDate,termDate
Dates can be either empty or null. 日期可以为空或为空。 I have to compare accordingly and put all the null/empty date values at the end.
我必须进行相应的比较,并将所有空/空日期值放在最后。
Collections.sort(usersList, new Comparator<User>() {
public int compare(User o1, User o2) {
if(o1.getTermDate() == null && o2.getTermDate() == null)
return 0; //They are both null, both equal
if(o1.getTermDate() == null && o2.getTermDate() != null)
return -1; // The first is null and the second is not, return the first as lower than the second
if(o1.getTermDate() != null && o2.getTermDate() == null)
return 1; //The first is not null and the second is, return the first as higher than the second
else
return o1.getTermDate().compare(o2.getTermDate()); //Return the actual comparison
}
});
It is not compiling compare method. 它不是编译比较方法。
Please guide. 请指导。
You should change return o1.getTermDate().compare(o2.getTermDate());
您应该更改
return o1.getTermDate().compare(o2.getTermDate());
to return o1.getTermDate().compareTo(o2.getTermDate());
return o1.getTermDate().compareTo(o2.getTermDate());
Because the class java.util.Date;
因为类
java.util.Date;
has not method called compare
. 没有名为
compare
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.