3
List<PackageInfo> pkginfoList = getPackageManager()
                .getInstalledPackages(0);

How to Sort the PackageInfo, i did ApplicationInfo thats working fine

Collections.sort(installedList, new ApplicationInfo.DisplayNameComparator(packageManager));

but i want implement PackageInfo also..

i'm not sure how to do.. Please any one help me...!

Manuel Selva
  • 18,554
  • 22
  • 89
  • 134
Karthick
  • 183
  • 3
  • 14

3 Answers3

2

Collections.sort method takes two parameters:

  1. The list you want to sort
  2. And a Comparator object used to compare elements which type is the type of elements in your list.

In your case you want to implement a Comparator<PackageInfo>. An example supposing a PackagInfo has a getName() method:

new Comparator<PackagInfo>() {
    @Override
    public int compare(PackagInfo arg0, PackagInfo arg1) {
      return arg0.getName().compareTo(arg1.getName());
    }
 };

Another solution is to get one from somewhere but I don't know Android well. Looking at the piece of code you provided, maybe you have a PackageInfo.xxxx static field which type is Comparator<PackageInfo> ?

Manuel Selva
  • 18,554
  • 22
  • 89
  • 134
2

Since Manuel Selva's answer didn't really work for me, here's what I did to improve it - assuming you want to sort the packages according to their app's name:

Collections.sort(packages, new Comparator<PackageInfo>() {
    @Override
    public int compare(PackageInfo arg0, PackageInfo arg1) {
        CharSequence name0 = arg0.applicationInfo.loadLabel(Context.getPackageManager());
        CharSequence name1 = arg1.applicationInfo.loadLabel(Context.getPackageManager());
        if (name0 == null && name1 == null) {
            return 0;
        }
        if (name0 == null) {
            return -1;
        }
        if (name1 == null) {
            return 1;
        }
        return name0.toString().compareTo(name1.toString());
    }
});
Yoav Feuerstein
  • 1,925
  • 2
  • 22
  • 53
-1

I think what you are looking for is already answered in this post.

Did you try looking for existing posts with the solution before creating a new question ?

Community
  • 1
  • 1
Viral Patel
  • 32,418
  • 18
  • 82
  • 110
  • Thanks for your reply.. ok buddy.. i searched but never get the answer thats way i posted my queries. i will look at on as your suggestion – Karthick Mar 18 '14 at 08:01