具有下面的代码,可以根据expiryDate对商品进行排序,但是它不起作用,商品仍然没有排序。 响应无论是升序还是降序都保持无序。
case OfferExpiryDate:
Comparator<OfferVO> comparatorAscEX = (o1, o2) -> {
if(o1.getOfferExpiryDate()==null && o2.getOfferExpiryDate()==null){
return 0;
}
else if(o1.getOfferExpiryDate()!=null){
int result =
ServiceUtility.compareDates
(ServiceUtility.convertStrToDate(o1.getOfferExpiryDate()),
o1.getTimeZoneOffSetInMilliSeconds(),
ServiceUtility.convertStrToDate
(o2.getOfferExpiryDate()),
o2.getTimeZoneOffSetInMilliSeconds());
return -result;
}
else{
return -1;
}
};
Comparator<OfferVO> comparatorDescEX = (o1, o2) -> {
if(o1.getOfferExpiryDate()==null && o2.getOfferExpiryDate()==null){
return 0;
}
else if(o2.getOfferExpiryDate()!=null){
int result = ServiceUtility.compareDates
(ServiceUtility.convertStrToDate(o2.getOfferExpiryDate()),
o2.getTimeZoneOffSetInMilliSeconds(),
ServiceUtility.convertStrToDate(o1.getOfferExpiryDate()),
o1.getTimeZoneOffSetInMilliSeconds());
return -result;
}
else{
return -1;
}
};
if(SortOrder.asc.equals(sortOrder)){
Collections.sort(offersList, comparatorAscEX);
}else{
Collections.sort(offersList, comparatorDescEX);
}
break;
下面是ServiceUtility类的比较方法:
public static int compareDates(Date date1, long timezone1, Date date2, long
timezone2) {
if (timezone1 == 1) {
// Calculate based on date only.
if (date1.before(date2)) {
return 1;
} else if (date1.after(date2)) {
return -1;
}
} else {
// Calculate based on date and timezone.
long timezoneDiff = timezone1 - timezone2;
long dateDiff = date1.getTime() - date2.getTime();
if (dateDiff - timezoneDiff < 0) {
return 1;
} else if (dateDiff - timezoneDiff > 0) {
return -1;
}
}
return 0;
}
无法解决问题,而ActiveDate则面临该问题。 请帮助我 谢谢。