title: Sorting the contents of ArrayAdapter or ArrayList
body: <p>I am working on android project and am making using of a ListView that retrieves data from the SQLite database. </p>
  
  <p>I am making a dataset using an ArrayList and then adding this ArrayList into an ArrayAdapter.</p>
  
  <p>When the data is being retrieved from the database, I am telling SQLite to do the sorting so everything is in alphabetical order when it is added into the ListView. At certain times, the information will be added dynamically to to the ListView without it requiring to re-fetch everythin from the database again. However, I want to keep everything in alphabetical order. </p>
  
  <p>How would I do this, do I sort the DataSet and then call the notifyDataSet Changes or do I do the sort directly on the ArrayAdapter. I've looked into performing the sort on the ArrayAdapter but this wants an argument that uses a Comparator but not sure what this is and can't find any working examples that may be of any help for what I want to achieve. </p>
  
  <p>Below is the code that populates the array and sets the list adapter</p>
  
  <pre><code>ArrayList&lt;Spanned&gt; passwords = managePasswordList.getPasswordList();
          if (passwords != null &amp;&amp; passwords.size() &gt; 0)
          {
              passwordArrayAdapter = new ArrayAdapter&lt;Spanned&gt;(getActivity().getApplicationContext(), 
                      android.R.layout.simple_list_item_activated_1, passwords);
              setListAdapter(passwordArrayAdapter);
              myListView.setTextFilterEnabled(true);
              txtNoRecords.setVisibility(View.GONE);
          }
          else
          {
              txtNoRecords.setVisibility(View.VISIBLE);
          }
  </code></pre>
  
  <p>I am then adding data to the dataset and refreshing the list view using the following</p>
  
  <pre><code>String company = Encryption.decrypt(passwords.get(i).company);
                      String username = Encryption.decrypt(passwords.get(i).username);
                      details = Html.fromHtml(company + "&lt;br /&gt;" + "&lt;small&gt;&lt;font color=\"#767676\"&gt;" + username + "&lt;/b&gt;&lt;/small&gt;");    
  </code></pre>
  
  <p>passwords.add(details);<br>
                          passwordArrayAdapter.notifyDataSetChanged();</p>
  
  <p>Thanks for any help you can provide. </p>
  
  <p><strong>UPDATE 1</strong>
  I've tried doing what Nick Bradbury suggested but I am having a problem with the comparator. I have the following code but I don't know where to go from here.</p>
  
  <pre><code>SQLiteDatabase myDb = null;
          Cursor cursor = null;
          ArrayList&lt;Spanned&gt; passwords = new ArrayList&lt;Spanned&gt;();
          try
          {
              myDb = context.openOrCreateDatabase("PasswordManager", Context.MODE_PRIVATE, null);
              cursor = myDb.rawQuery("SELECT * FROM password ASC", null);
              while (cursor.moveToNext())
              {
                  final String company = Encryption.decrypt(cursor.getString(2));
                  final String username = Encryption.decrypt(cursor.getString(4));
                  Spanned details = Html.fromHtml(company + "&lt;br /&gt;" + "&lt;small&gt;&lt;font color=\"#767676\"&gt;" + username + "&lt;/b&gt;&lt;/small&gt;");
                  passwords.add(details);
  
                  Collections.sort(passwords, new Comparator&lt;Spanned&gt;() {
  
                      public int compare(Spanned lhs, Spanned rhs) {
                          return 0;
                      }
                  });
              }
          }
          catch (SQLiteException ex)
          {
              common.showBasicAlertDialog("Unfortunately something has gone wrong.\n\nWe will fix this as soon as we can", false);
              Log.e("Database Error", ex.toString());
              return null;
          }
  </code></pre>
  
  <p>In the return statement I have no idea what to do, I've tried <code>return lhs.compareTo</code> but the lhs and rhs variables don't have the compareTo function so I have not got a clue what to do. </p>