I wrote my own comparator with compare method. When i compile my app i get this error:
IllegalArgumentException: Comparison method violates its general contract!
o1Id and o2Id are BigInteger type, date01 and date02 are Date type
int result = 0;
if(o1Id == null){
result = 1;
} else if(o2Id == null){
result = -1;
} else if (o1Id != null && o2Id != null) {
if (dateO1 != null && dateO2 != null) {
result = dateO1.compareTo(dateO2);
if (result == 0) {
result = o1Id.compareTo(o2Id);
}
} else {
result = o1Id.compareTo(o2Id);
}
}
return result;
Question: How to improve my code to fix this error?