0

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?

John Doe
  • 319
  • 2
  • 5
  • 24
  • can u share implementation of compareTo ? what type of obj date is? – Ankit Apr 11 '19 at 07:39
  • Do you understand the requirements of the general contract? (Also, this isn't the entirety of the method: at a minimum, you're not showing how null `o1Id` and `o2Id` are handled. Please show it all). – Andy Turner Apr 11 '19 at 07:41

0 Answers0