2

What I Have

enum FileItemComparator implements Comparator<FileItem> {

    //Using ENUM
    NAME_SORT {
        public int compare(FileItem o1, FileItem o2) {

            int result = 0;
            if (o1 != null && o2 != null) {

                String n1 = o1.getFileName();
                String n2 = o2.getFileName();

                if (n1 != null && n2 != null)
                    result = n1.compareTo(n2);
            }

            return result;
        }
    },
    DATE_SORT {
        public int compare(FileItem o1, FileItem o2) {

            int result = 0;
            if (o1 != null && o2 != null) {

                String d1 = o1.getFileDate();
                String d2 = o2.getFileDate();

                if (d1 != null && d2 != null) {

                    Long l1 = Long.valueOf(d1);
                    Long l2 = Long.valueOf(d2);

                    if (l1 != null && l2 != null) {
                        result = l1.compareTo(l2);
                    }
                }

            }

            return result;
        }
    },
    SIZE_SORT {
        public int compare(FileItem o1, FileItem o2) {

            int result = 0;
            if (o1 != null && o2 != null) {

                File f1 = o1.getItem();
                File f2 = o2.getItem();

                if (f1 != null && f2 != null) {

                    result = Long.valueOf(f1.length()).compareTo(Long.valueOf(f2.length()));
                }
            }

            return result;
        }
    };

    public static Comparator<FileItem> descending(final Comparator<FileItem> other) {

        return new Comparator<FileItem>() {
            public int compare(FileItem o1, FileItem o2) {
                return -1 * other.compare(o1, o2);
            }
        };
    }

    public static Comparator<FileItem> getComparator(final FileItemComparator... multipleOptions) {
        return new Comparator<FileItem>() {
            public int compare(FileItem o1, FileItem o2) {
                for (FileItemComparator option : multipleOptions) {
                    int result = option.compare(o1, o2);
                    if (result != 0) {
                        return result;
                    }
                }
                return 0;
            }
        };
    }
}

I have this code which is used to sort the files based on name, date or size.

But I get this error,

java.lang.IllegalArgumentException: Comparison method violates its general contract!
at java.util.TimSort.mergeHi(TimSort.java:864)
at java.util.TimSort.mergeAt(TimSort.java:481)
at java.util.TimSort.mergeCollapse(TimSort.java:406)
at java.util.TimSort.sort(TimSort.java:210)
at java.util.TimSort.sort(TimSort.java:169)
at java.util.Arrays.sort(Arrays.java:2010)
at java.util.Collections.sort(Collections.java:1883)

Some of my users get this error and I see it in the crash reports. But I can not reproduce this error myself in any way.

Can anyone please help me in finding the problem. I have really seemed to have spent a lot of hours in this but couldn't find anything. Please help me in this?

Thanks in advance.

Vikasdeep Singh
  • 20,983
  • 15
  • 78
  • 104
Aritra Roy
  • 15,355
  • 10
  • 73
  • 107
  • A similar question is already answered: http://stackoverflow.com/questions/11441666/java-error-comparison-method-violates-its-general-contract – Gero Apr 20 '15 at 04:31
  • @Gero Yes, I saw this question before and can't seem to fix my problem. It would be great if you can help me solve it. – Aritra Roy Apr 20 '15 at 04:56

1 Answers1

1

Following this answer and searching for TimSort (as stated on your stacktrace), you can reach this bug report that may be your case.

As madth suggests, one workaround could be adding to your code:
System.setProperty("java.util.Arrays.useLegacyMergeSort", "true");

Community
  • 1
  • 1
Gero
  • 1,842
  • 25
  • 45
  • Using legacy merge sort should be the last option I think. In my code I have just used compareTo method and not my own logic. I dont know where I am doing wrong. – Aritra Roy Apr 20 '15 at 05:53