0
public int compare(File f1, File f2) {
            try {
                Date fileDateTime1 = InterimUtil.extractDate(InterimUtil.fileDateFormat, f1);
                Date fileDateTime2 = InterimUtil.extractDate(InterimUtil.fileDateFormat, f2);
                if (fileDateTime1.getTime() > fileDateTime2.getTime()) {
                    return 1;
                }
                return -1;
            } catch (Exception e) {
                NGVSInterimFileGenerator.LOGGER.severe("Error occured in comparator" + e.getMessage());
            }
            return -1;
        }
    };
    Arrays.sort(files, fileModTimeComp);

Error on executing above code. above code is gving the below error.

java.lang.IllegalArgumentException: Comparison method violates its general contract! at java.util.TimSort.mergeHi(TimSort.java:899) at java.util.TimSort.mergeAt(TimSort.java:516) at java.util.TimSort.mergeCollapse(TimSort.java:441) at java.util.TimSort.sort(TimSort.java:245) at java.util.Arrays.sort(Arrays.java:1438)

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
NAGAPPA
  • 1
  • 2
  • java.lang.IllegalArgumentException: Comparison method violates its general contract! at java.util.TimSort.mergeHi(TimSort.java:899) at java.util.TimSort.mergeAt(TimSort.java:516) at java.util.TimSort.mergeCollapse(TimSort.java:441) at java.util.TimSort.sort(TimSort.java:245) at java.util.Arrays.sort(Arrays.java:1438) – NAGAPPA Dec 01 '17 at 10:39
  • Don't add information in comments but rather [edit] your question. – Thomas Dec 01 '17 at 10:39
  • 1
    Well, you never return zero, for one thing... – Andy Turner Dec 01 '17 at 10:40
  • 1
    As for your problem: the general contract is that if x > y then you _must_ get y < x if you call the comparator with swapped arguments (i.e. if you'd call `compare(f2,f1)`). If both dates were equal you'd get x < y _and_ y < x which isn't correct for sure. – Thomas Dec 01 '17 at 10:41
  • 1
    and I don't think that returning -1 in the case of an exception is a good idea. Just bail out, the resulting order of the elements won't be meaningful. – Andy Turner Dec 01 '17 at 10:42
  • @Thomas sure man – NAGAPPA Dec 01 '17 at 13:51

0 Answers0