After trying for 3 hours all the solutiond that I found with no success, I'm posting my issue here. I am getting the exception:
java.lang.IllegalArgumentException: Comparison method violates its general contract
This is my code:
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;
}