<分区>
在尝试了 3 个小时后,我发现所有的解决方案都没有成功,我在这里发布我的问题。我收到异常:
java.lang.IllegalArgumentException: 比较方法违反了它的一般契约
这是我的代码:
public int compare(InstrumentModel o1, InstrumentModel o2) {
int c = 0;
if(c == 0 && o1.getUnderlyingAsset()!=null) {
c = o1.getUnderlyingAsset().compareTo(o2.getUnderlyingAsset());
}
if(c == 0 && o1.getSymbol()!=null) {
c = o1.getSymbol().compareTo(o2.getSymbol());
}
if(c == 0 && o1.getExpiryDateInDate()!=null && o2.getExpiryDateInDate()!=null) {
DateFormat df = new SimpleDateFormat("yyyy-mm-dd");
Date date1 = null;
Date date2 = null;
try {
date1 = df.parse(df.format(o1.getExpiryDateInDate()));
date2 = df.parse(df.format(o2.getExpiryDateInDate()));
} catch (ParseException e) {
e.printStackTrace();
}
c = date1.before(date2) ? 1 : date1.after(date2) ? -1 : 0 ;
}
return c;
}