-1

I have a listview, which I would like to sort in an alphabetic order. How do I do that?

public class AppDrawer extends AppCompatActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    getSupportActionBar().hide();
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_app_drawer);
    ListView userInstalledApps = (ListView) findViewById(R.id.installed_app_list);
    List<AppList> installedApps = getInstalledApps();
    AppAdapter installedAppAdapter = new AppAdapter(this, installedApps);
    userInstalledApps.setAdapter(installedAppAdapter);
}

private List<AppList> getInstalledApps() {
    List<AppList> res = new ArrayList<AppList>();
    List<PackageInfo> packs = getPackageManager().getInstalledPackages(0);
    for (int i = 0; i < packs.size(); i++) {
        PackageInfo p = packs.get(i);
        if ((isSystemPackage(p) == false)) {
            String appName = p.applicationInfo.loadLabel(getPackageManager()).toString();
            Drawable icon = p.applicationInfo.loadIcon(getPackageManager());
            res.add(new AppList(appName, icon));
        }
    }
    return res;
}

private boolean isSystemPackage(PackageInfo pkgInfo) {
    return ((pkgInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) ? true : false;
}
  • sort `installedApps` list – Basi Dec 12 '18 at 05:55
  • @Basi I don't know what that means and how to use that. Can you please elaborate? –  Dec 12 '18 at 05:56
  • please look at [this](https://stackoverflow.com/questions/16252269/how-to-sort-an-arraylist) – Basi Dec 12 '18 at 05:58
  • @mrbean : listview is fed with list of data, if you want to sort the listview then you have to first sort the list of data and then invalidate the listview. – Vinodh Dec 12 '18 at 06:08
  • Possible duplicate of [Sort ArrayList of custom Objects by property](https://stackoverflow.com/questions/2784514/sort-arraylist-of-custom-objects-by-property) – ADM Dec 12 '18 at 06:13

3 Answers3

0

You can use Comparator for sorting a list.

 List<AppList> installedApps = getInstalledApps();

    Collections.sort(installedApps , new Comparator<AppList>() {
        public int compare(AppList v1, AppList v2) {
            return v1.getAppName().compareTo(v2.getAppName());
        }
    });

Or if you are using Java 8:

list.sort(String::compareToIgnoreCase);
Sabyasachi
  • 3,499
  • 2
  • 14
  • 21
  • I have to cast it to string ` Collections.sort((List) userInstalledApps, new Comparator() {` to make it error free –  Dec 12 '18 at 06:05
  • and it crashes. Exception.. cannot cast list to a string –  Dec 12 '18 at 06:05
0

You can use comparator for sorting your list. change your getInstalledApps() method as below.

private List<AppList> getInstalledApps() {
    List<AppList> res = new ArrayList<AppList>();
    List<PackageInfo> packs = getPackageManager().getInstalledPackages(0);
    for (int i = 0; i < packs.size(); i++) {
        PackageInfo p = packs.get(i);
        if ((isSystemPackage(p) == false)) {
            String appName = p.applicationInfo.loadLabel(getPackageManager()).toString();
            Drawable icon = p.applicationInfo.loadIcon(getPackageManager());
            res.add(new AppList(appName, icon));
        }
    }
    Collections.sort(res, new Comparator<AppList>() {
        @Override
        public int compare(AppList o1, AppList o2) {
            return o1.getAppName().compareTo(o2.getAppName());  // use your getter for getting app name you are setting.
        }
    });
    return res;
}
karan
  • 8,637
  • 3
  • 41
  • 78
  • I have mentioned in answer, you need to use the getter method that you have defined in your applist class to get app name attribute – karan Dec 12 '18 at 06:29
0

sort method of List interface is mutable operation. Your list will be modified and order property of List will be corrupted better to use Streams of Java 8

List.stream().sorted().collect(Collectors.toList());

pass your Comparator in sorted method and get the list.