Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort out a List<PackageInfo> in android..?

Tags:

java

android

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...!

like image 806
Karthick Avatar asked Dec 31 '25 14:12

Karthick


1 Answers

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> ?

like image 64
Manuel Selva Avatar answered Jan 02 '26 03:01

Manuel Selva



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!