2

I have the following code:

packageManager = getPackageManager();
    List<PackageInfo> packageList = packageManager.getInstalledPackages(PackageManager.GET_PERMISSIONS);
    List<PackageInfo> installedapps = new ArrayList<PackageInfo>();

    for(PackageInfo apps: packageList){
        if(!isSystemPackage(apps)){
            installedapps.add(apps);
        }
    }


   Collections.sort(installedapps, new Comparator<PackageInfo>(){
        public int compare(PackageInfo o1, PackageInfo o2) {
            return o1.packageName.compareTo(o2.packageName);
        }
    });



    apkList = (ListView) findViewById(R.id.listView);
    apkList.setAdapter(new AppInfoAdapter(this, installedapps, packageManager));

installedapps is a list of all the apps on the device minus the system apps. The only thing I want to do is sort them alphabetically, can't quite figure out how.

scibor
  • 983
  • 4
  • 12
  • 21
  • Just sort your list before passing it to your adapter. http://blog.vogella.com/2009/08/04/collections-sort-java/ – AMerle Jun 04 '13 at 18:02
  • I'm just confused by what I'm comparing, I used: Collections.sort(installedapps, new Compater etc. What I'm confused about is what I should be comparing for the two PackageInfo objects – scibor Jun 04 '13 at 18:14

2 Answers2

1

From what I understand, you want to sort a list of PackageInfo objects alphabetically by name. I would start here. What you want to do is create a custom Comparator for the ArrayList you called installedapps and then sort is using Collections.sort(..). The property of PackageInfo you should use for sorting is p.packageName which is the fully qualified name of the Application (you can use regular expressions to isolate the last piece of the package name). An example of isolating cosmetic properties of apps you can find here.

Community
  • 1
  • 1
Sandile
  • 90
  • 1
  • 5
  • Added the Collections.sort() as suggested, that was the idea I had earlier, and now I'm moreso convinced I'm on the right track, but this isn't working. Are there any glaring mistakes, or is this right and I should maybe look elsewhere for a mistake? – scibor Jun 04 '13 at 18:25
  • 1
    Got it! I was looking to compare applicationInfo.loadLabel(getPackageManager()).toString() instead of .packageName – scibor Jun 04 '13 at 18:40
  • Note that if you want to compare PackageInfo (as mentioned in the original question), not ApplicationInfo, then my answer here might be useful for you: https://stackoverflow.com/a/47198817/997940 – Yoav Feuerstein Nov 09 '17 at 10:07
0

Android provides an ApplicationInfo.DisplayNameComparator which can be used for sorting of

List<ApplicationInfo>

Check out: Alphabatize list of installed apps

Community
  • 1
  • 1
Srikant Sahay
  • 885
  • 6
  • 9