1

This is my code that throws the exception down below:

Arrays.sort(listFile, new Comparator<File>() {
        public int compare(File f1, File f2) {
               return Long.valueOf(f1.lastModified()).compareTo(f2.lastModified());
        }
});

I think it might be a Java language change in Java 7, but I don't know what I have to change that it will work correctly. Thank you very much!

Fatal Exception: java.lang.IllegalArgumentException: Comparison method violates its general contract!
   at java.util.TimSort.mergeLo(TimSort.java:743)
   at java.util.TimSort.mergeAt(TimSort.java:479)
   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 de.munichsdorfer.screenittrial.contentobserver.MyContentObserver.onChange(MyContentObserver.java:51)
   at android.database.ContentObserver.onChange(ContentObserver.java:145)
   at android.database.ContentObserver$NotificationRunnable.run(ContentObserver.java:216)
   at android.os.Handler.handleCallback(Handler.java:739)
   at android.os.Handler.dispatchMessage(Handler.java:95)
   at android.os.Looper.loop(Looper.java:145)
   at android.os.HandlerThread.run(HandlerThread.java:61)

UPDATE: Changed the code to

return Long.valueOf(f1.lastModified()).compareTo(Long.valueOf(f2.lastModified()));

and now this is showing:

enter image description here

1 Answers1

1

You are converting the first lastModified to a Long, but not the second. Try this:

return Long.valueOf(f1.lastModified()).compareTo(Long.valueOf(f2.lastModified()));
Buddy
  • 10,874
  • 5
  • 41
  • 58
  • mhh, I changed it according to your suggestion, but now Android Studio gives some suggestion. I edited my post. – Johannes Muenichsdorfer Jul 15 '16 at 14:35
  • Autoboxing takes care of that conversion. *Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes* – Blackbelt Jul 15 '16 at 14:38