如何在短时间内对ApplicationInfo类型的列表进行排序?

问题描述 投票:0回答:1

我想对ApplicationInfo类型的列表进行排序,我想对ApplicationInfo类型的列表进行排序,以便用户应用程序在前,然后是系统应用程序(对于Android),同时减少排序过程所需的时间。

我用了一个方法,但是排序需要5秒多。我想减少这个时间

代码:

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;
         }
     });
java android arraylist android-recyclerview android-applicationinfo
1个回答
0
投票

怎么样:

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

如果[方法]

isSystem
的结果对于
o1
o2
不相同,则返回-1(减一),否则返回[方法]
loadLabel
返回的字符串比较结果。

请注意,这两个

if
(来自您的代码)本质上是检查
isSystem(o1)
是否返回与
isSystem(o2)
不同的结果。

if (!isSystem(o1) && isSystem(o2))return -1;
if (isSystem(o1) && !isSystem(o2))return 1;
© www.soinside.com 2019 - 2024. All rights reserved.