javaandroidarraylistandroid-recyclerviewandroid-applicationinfo

How to sort a list of type ApplicationInfo in a short period of time?


I want to sort a list of the ApplicationInfo type، I want to sort a list of the ApplicationInfo type so that the user applications are first, then the system applications (for Android), while reducing the time period required by the sorting process.

I used a method, but it takes more than 5 seconds to sort. I want to reduce this time

Code:

Collections.sort(
     list,
     new Comparator<ApplicationInfo>(){
         @Override
         public int compare(ApplicationInfo o1, ApplicationInfo o2) {
             if (!isSystem(o1) && isSystem(o2))return -1;
             if (isSystem(o1) && !isSystem(o2))return 1;
             String label1 = o1.loadLabel(pm).toString();
             String label2 = o2.loadLabel(pm).toString();
             return label1.compareToIgnoreCase(label2);
         }
         boolean isSystem(ApplicationInfo app) {
             return (app.flags & ApplicationInfo.FLAG_SYSTEM) == ApplicationInfo.FLAG_SYSTEM;
         }
     });

Solution

  • How about:

    boolean b1 = isSystem(o1);
    boolean b2 = isSystem(o2);
    if (b1) {
        if (b2) {
            return o1.loadLabel(pm).toString().compareToIgnoreCase(o2.loadLabel(pm).toString());
        }
        else {
            return 1;
        }
    }
    else {
        if (b2) {
            return -1;
        }
        else {
            return o1.loadLabel(pm).toString().compareToIgnoreCase(o2.loadLabel(pm).toString());
        }
    }
    

    Method isSystem is called twice (rather than four times as in your code).