java.lang.NoSuchMethodError: No interface method sort(Ljava/util/Comparator;) exception in sorting arraylist android-ThrowExceptions

Exception or error:

I’m trying to sort an ArrayList in Java in Android app but I’m getting this weird exception.

Code:

eventsList.sort(new Comparator<Event>() {
        @Override
        public int compare(Event event, Event t1) {
            return event.getEventStartDate().compareTo(t1.getEventStartDate());
        }
    });

Exception:

java.lang.NoSuchMethodError: No interface method sort(Ljava/util/Comparator;)V in class Ljava/util/List; or its super classes (declaration of 'java.util.List' appears in /system/framework/core-libart.jar)
How to solve:

ArrayList#sort() was added in API level 24 and runtimes below API level 24 don’t have that method. Looks like your compileSdkVersion is at 24 so you got the code to compile in the first place.

Use Collections.sort(list, comparator) instead.

###

Zxing?

If you get this error in the Zxing core lib in com.google.zxing.qrcode.detector.FinderPatternFinder.selectBestPatterns you can solve it by downgrading Zxing to 3.3.x (3.3.3 currently).

See https://github.com/zxing/zxing/issues/1170 for details.

###

What if you try

Collections.sort(eventsList, new Comparator...

As far as I know ArrayList doesn’t have sort method.

###

List doesn’t have its own sorting method, you’ll need to call

Collections.sort() 

as the method on the list. If this returns a ClassCastError, that means the list has non-sortable items. I think this should fix it, but without full code, it’s hard to check.

###

ArrayList.sort() method was added later that’s why its not working. We can update java version (1.8) or we can use below method.

Collections.sort(eventList, new Comparator<Event>() {
        @Override
        public int compare(Event event, Event t1) {
            return event.getEventStartDate().compareTo(t1.getEventStartDate());
        }
});

Leave a Reply

Your email address will not be published. Required fields are marked *