I have below code-
Stream.of(objects).sorted((o1,o2) -> {
if (o1.getIsXXXTrue()) return -1;
if (o2.getIsXXXTrue()) return 1;
return (o2.getScore() - o1.getScore());
}).toArray(MyObject[]::new);
Intermittently for some set of objects I am getting below exception-
java.lang.IllegalArgumentException: Comparison method violates its general contract!
I read some similar article and came to know that it might be because transitivity contract is breaking in comparison code(A > B and B > C but A is not greater than C). I tried to dry run with different values of objects but couldn't find any case where transitivity might break! Can you you guys help me with what might be wrong with this code! Is it about transitivity or any other contract might be breaking. Any leads to find the problem and how to fix it are appreciated. Thank you.