0

I think my comparator below is good enough to not throw below exception:

> Caused by: java.lang.IllegalArgumentException: Comparison method violates its general contract!
    at java.util.TimSort.mergeLo(TimSort.java:777)
    at java.util.TimSort.mergeAt(TimSort.java:514)
    at java.util.TimSort.mergeForceCollapse(TimSort.java:457)
    at java.util.TimSort.sort(TimSort.java:254)
    at java.util.Arrays.sort(Arrays.java:1512)
    at java.util.ArrayList.sort(ArrayList.java:1462)

segmentedRecords.sort((r1, r2) -> {
      if ((Short) r1.getFieldAtIndex(0) >= (Short) r2.getFieldAtIndex(0))
        return 1;
      else
        return -1;
    } 
Gaurav
  • 157
  • 1
  • 2
  • 8
  • Note that casting to `Short` is unnecessary: `>=` means that the `Short` operands are unboxed and widened to `int`. So, just cast to `short` (or just don't cast at all, unless you need unusual overflow behavior). – Andy Turner Mar 22 '18 at 14:45
  • Your method never returns zero, which it must if `r1 == r2`. In other words, it's not reflexive. – Andy Turner Mar 22 '18 at 14:47

0 Answers0