title: Alphabetize List of Installed Apps
body: <p>I have a list of the users installed applications and want it to come out already alphabetized (not on dividers or anything like that just already alphabetized) I looked around but I haven't really found anything like this (it has all been with dividers). I feel like this should be a simple thing to do but I haven't been able to figure it out. Here is my code:</p>
  
  <p>Utilities.java:</p>
  
  <pre><code>package com.example.awesomefilebuilderwidget;
  
  IMPORTS
  
  public class Utilities {
  
  /*
   * Get all installed application on mobile and return a list
   * @param   c   Context of application
   * @return  list of installed applications
   */
  public static List&lt;ResolveInfo&gt; getInstalledApplications(Context c) {
      Intent intent = new Intent(Intent.ACTION_MAIN);
      intent.addCategory(Intent.CATEGORY_LAUNCHER);
      return c.getPackageManager().queryIntentActivities(intent, PackageManager.GET_ACTIVITIES);
  }
  </code></pre>
  
  <p>AppInfoAdapter.java:</p>
  
  <pre><code>package com.example.awesomefilebuilderwidget;
  
   IMPORTS
  
  public class AppInfoAdapter extends BaseAdapter implements Filterable {
  private Context mContext;
  private List&lt;ResolveInfo&gt; mListAppInfo;
  private PackageManager mPackManager;
  private List&lt;ResolveInfo&gt; originalListAppInfo;
  
  public AppInfoAdapter(Context c, List&lt;ResolveInfo&gt; listApp,
          PackageManager pm) {
      mContext = c;
      this.originalListAppInfo = this.mListAppInfo = listApp;
      mPackManager = pm;
      Log.d("AppInfoAdapter", "top");
  }
  
  @Override
  public int getCount() {
      Log.d("AppInfoAdapter", "getCount()");
      return mListAppInfo.size();
  }
  
  @Override
  public Object getItem(int position) {
      Log.d("AppInfoAdapter", "getItem");
      return mListAppInfo.get(position);
  }
  
  @Override
  public long getItemId(int position) {
      Log.d("AppInfoAdapter", "getItemId");
      return position;
  }
  
  @Override
  public View getView(final int position, View convertView, ViewGroup parent) {
      // get the selected entry
      final ResolveInfo entry = (ResolveInfo) mListAppInfo.get(position);
  
      // reference to convertView
      View v = convertView;
  
      // inflate new layout if null
      if (v == null) {
          LayoutInflater inflater = LayoutInflater.from(mContext);
          v = inflater.inflate(R.layout.layout_appinfo, null);
          Log.d("AppInfoAdapter", "New layout inflated");
      }
  
      // load controls from layout resources
      ImageView ivAppIcon = (ImageView) v.findViewById(R.id.ivIcon);
      TextView tvAppName = (TextView) v.findViewById(R.id.tvName);
      TextView tvPkgName = (TextView) v.findViewById(R.id.tvPack);
      final CheckBox addCheckbox = (CheckBox) v
              .findViewById(R.id.addCheckbox);
      Log.d("AppInfoAdapter", "Controls from layout Resources Loaded");
  
      // set data to display
      ivAppIcon.setImageDrawable(entry.loadIcon(mPackManager));
      tvAppName.setText(entry.activityInfo.loadLabel(mPackManager));
      tvPkgName.setText(entry.activityInfo.packageName);
  
  }
  </code></pre>
  
  <p>Drag_and_Drop_App:</p>
  
  <pre><code>// create new adapter
      adapter = new AppInfoAdapter(this, (List&lt;ResolveInfo&gt;) Utilities.getInstalledApplications(this), getPackageManager());
      // load list application
     mListAppInfo = (ListView)findViewById(R.id.lvApps);
      // set adapter to list view
      mListAppInfo.setAdapter(adapter);
  </code></pre>
  
  <p>So what would be the best way to alphabetize this list?</p>
  
  <p>UPDATED:</p>
  
  <p>Final answer:</p>
  
  <pre><code>    public static ArrayList&lt;ResolveInfo&gt; getInstalledApplications(Context c) {
      Intent intent = new Intent(Intent.ACTION_MAIN);
      intent.addCategory(Intent.CATEGORY_LAUNCHER);
      final PackageManager pm = c.getPackageManager();
      ArrayList&lt;ResolveInfo&gt; applist = (ArrayList&lt;ResolveInfo&gt;) c.getPackageManager().queryIntentActivities(intent, PackageManager.GET_ACTIVITIES);
      Collections.sort(applist, new Comparator&lt;ResolveInfo&gt;(){
            public int compare(ResolveInfo emp1, ResolveInfo emp2) {
              return emp1.loadLabel(pm).toString().compareToIgnoreCase(emp2.loadLabel(pm).toString());
            }
          });
      return applist;
  }
  </code></pre>