title: ArrayList sorting where data is in from SQLite
body: <p>i want to sort arraylist in android where data is adding over database.
          how can i do that please help me.</p>
  
  <pre><code>    import android.content.ContentValues;
      import android.content.Context;
      import android.database.Cursor;
      import android.database.sqlite.SQLiteDatabase;
      import android.provider.ContactsContract;
  
      import java.util.ArrayList;
      import java.util.Collections;
      import java.util.Comparator;
  
      /**
       * Created by Imran on 10/25/2015.
       */
      public class DataSource {
  
          private DatabaseHelper databaseHelper;
  
          private SQLiteDatabase database;
  
          private ContactModel contactModel;
  
  
          public DataSource(Context context) {
              databaseHelper = new DatabaseHelper(context);
          }
  
          public void open(){
              database= databaseHelper.getWritableDatabase();
          }
          public void close(){
              databaseHelper.close();
          }
  
          public long insertData(ContactModel contactModel){
              this.open();
              ContentValues contentValues=new ContentValues();
  
              contentValues.put(DatabaseHelper.COL_NAME,contactModel.getName());
  
              contentValues.put(DatabaseHelper.COL_COMPANY,contactModel.getCompanyName());
  
              contentValues.put(DatabaseHelper.COL_DESIGNATION,contactModel.getDesignation());
  
              contentValues.put(DatabaseHelper.COL_PHONE_NO,contactModel.getPhoneNo());
  
              contentValues.put(DatabaseHelper.COL_EMAIL,contactModel.getEmail());
  
  
  
              long inserted=database.insert(DatabaseHelper.CONTACT_TABLE,null,contentValues);
              database.close();
              this.close();
  
              return inserted;
          }
  
  
          public boolean updateData(String id,ContactModel contactModel){
  
  
              this.open();
              ContentValues contentValues=new ContentValues();
  
              contentValues.put(DatabaseHelper.COL_NAME,contactModel.getName());
  
              contentValues.put(DatabaseHelper.COL_COMPANY,contactModel.getCompanyName());
  
              contentValues.put(DatabaseHelper.COL_DESIGNATION,contactModel.getDesignation());
  
              contentValues.put(DatabaseHelper.COL_PHONE_NO,contactModel.getPhoneNo());
  
              contentValues.put(DatabaseHelper.COL_EMAIL, contactModel.getEmail());
  
              long update=database.update(DatabaseHelper.CONTACT_TABLE, contentValues, DatabaseHelper.COL_ID + " = " + id,
                      null);
  
              if (update&gt;0){
                  return true;
              }else {
                  return false;
              }
  
          }
  
          public ArrayList&lt;ContactModel&gt; getAllContact(){
              this.open();
              ArrayList&lt;ContactModel&gt; contactModelArrayList=new ArrayList&lt;&gt;();
              ArrayList&lt;ContactModel&gt; sortedContactModelArrayList=new ArrayList&lt;&gt;();
  
  
              Cursor cursor=database.query(DatabaseHelper.CONTACT_TABLE, null, null, null, null, null, null);
  
              if(cursor!=null &amp;&amp; cursor.getCount()&gt;0){
                  cursor.moveToFirst();
  
                  for(int i=0;i&lt;cursor.getCount();i++){
                      String id=cursor.getString(cursor.getColumnIndex(DatabaseHelper.COL_ID));
                      String name=cursor.getString(cursor.getColumnIndex(DatabaseHelper.COL_NAME));
                      String company=cursor.getString(cursor.getColumnIndex(DatabaseHelper.COL_COMPANY));
                      String designation=cursor.getString(cursor.getColumnIndex(DatabaseHelper.COL_DESIGNATION));
                      String phoneNo=cursor.getString(cursor.getColumnIndex(DatabaseHelper.COL_PHONE_NO));
                      String email=cursor.getString(cursor.getColumnIndex(DatabaseHelper.COL_EMAIL));
  
  
                      contactModel=new ContactModel(id,name,company,designation,phoneNo,email);
  
                      contactModelArrayList.add(contactModel);
                      cursor.moveToNext();
                      this.close();
  
  
                  }
              }
           /*   Collections.sort(contactModelArrayList, new Comparator&lt;&gt;() {
                  @Override
                  public int compare(String s1, String s2) {
                      return s1.compareToIgnoreCase(s2);
                  }
              });*/
  
              sortedContactModelArrayList=Collections.sort(contactModelArrayList);
  
              return sortedContactModelArrayList;
          }
  
          public boolean delete(String id){
              this.open();
              database.delete(DatabaseHelper.CONTACT_TABLE, DatabaseHelper.COL_ID + "=" + id, null);
              this.close();
              return true;
          }
  
          public ContactModel singleContact(String id){
              this.open();
              Cursor cursor=database.query(DatabaseHelper.CONTACT_TABLE,
                      new String[]{DatabaseHelper.COL_ID,DatabaseHelper.COL_NAME,DatabaseHelper.COL_COMPANY,DatabaseHelper.COL_DESIGNATION,
                              DatabaseHelper.COL_PHONE_NO,DatabaseHelper.COL_EMAIL},DatabaseHelper.COL_ID +" = " + id,null,null,null ,null);
              cursor.moveToFirst();
  
              String mId=cursor.getString(cursor.getColumnIndex(DatabaseHelper.COL_ID));
              String name=cursor.getString(cursor.getColumnIndex(DatabaseHelper.COL_NAME));
              String company=cursor.getString(cursor.getColumnIndex(DatabaseHelper.COL_COMPANY));
              String designation=cursor.getString(cursor.getColumnIndex(DatabaseHelper.COL_DESIGNATION));
              String phoneNo=cursor.getString(cursor.getColumnIndex(DatabaseHelper.COL_PHONE_NO));
              String email=cursor.getString(cursor.getColumnIndex(DatabaseHelper.COL_EMAIL));
              cursor.close();
              contactModel=new ContactModel(mId,name,company,designation,phoneNo,email);;
              this.close();
              return contactModel;
          }
  
  
      }
  </code></pre>
  
  <p>i have done project where everything is working but can not make sort of data(name) alphatically</p>