title: Get sorting order preference selected user in Contacts>settings in Android Device and How to reduce query time?
body: <p>I have two questions:</p>
  
  <p><strong>1:</strong> How can my application know Default sorting order used for sorting native "Android Device Contacts"?</p>
  
  <p>Inside "Contacts" -> "Settings" in android, we have "<strong>List by</strong>" and "<strong>Display Contacts by"</strong> options. How can I get these prefrences in my application.</p>
  
  <p>For example: suppose device contacts are sorted by "First Name", then application should get some constant(or something similar). On the other hand, if contacts are sorted by "Last Name" then application should get constants(or info) regarding the same.</p>
  
  <p>I searched SO and I got this <a href="https://stackoverflow.com/q/14357115/2563740">link</a>, but the problem is that the solution was <strong>deprecated</strong> in <strong>API level 5</strong>.</p>
  
  <p>Here is my query for fetching contacts</p>
  
  <pre><code>String userPreference = getPrefContacts();/* **getPrefContacts() will return either ContactsContract.Data.DATA2 or ContactsContract.Data.DATA3** */
      try {
                  ContentResolver cr = getActivity().getContentResolver();
                  String[] projectionName = new String[] {
                          ContactsContract.Data.CONTACT_ID,
                          ContactsContract.Data.DATA2, ContactsContract.Data.DATA5,
                          ContactsContract.Data.DATA3, ContactsContract.Data.DATA1 };
                  String sortOrder = userPrefrence
                          + " COLLATE LOCALIZED ASC";
  
                  Cursor nameCursor = cr.query(ContactsContract.Data.CONTENT_URI,
                          projectionName, null, null, sortOrder);
                  nameCursor.moveToFirst();
                  if (nameCursor.getCount() &gt; 0) {
                      do {
                          String fName = nameCursor
                                  .getString(nameCursor
                                          .getColumnIndexOrThrow(ContactsContract.Data.DATA2));
                          String mName = nameCursor
                                  .getString(nameCursor
                                          .getColumnIndexOrThrow(ContactsContract.Data.DATA5));
                          String lName = nameCursor
                                  .getString(nameCursor
                                          .getColumnIndexOrThrow(ContactsContract.Data.DATA3));
                          String name = nameCursor
                                  .getString(nameCursor
                                          .getColumnIndexOrThrow(ContactsContract.Data.DATA1));
                          if (name != null) {
                              String[] projectionCommon = new String[] {
                                      ContactsContract.Contacts._ID,
                                      ContactsContract.Contacts.HAS_PHONE_NUMBER };
                              String selectionCommon = ContactsContract.Contacts.DISPLAY_NAME
                                      + " = ?";
                              String[] selectionArgCommon = new String[] { name };
  
                              Cursor common = cr.query(
                                      ContactsContract.Contacts.CONTENT_URI,
                                      projectionCommon, selectionCommon,
                                      selectionArgCommon, null);
                              if (common.getCount() &gt; 0) {
                                  while (common.moveToNext()) {
                                      String contactID = common
                                              .getString(common
                                                      .getColumnIndexOrThrow(ContactsContract.Contacts._ID));
                                      int hasPhone = common
                                              .getInt(common
                                                      .getColumnIndexOrThrow(ContactsContract.Contacts.HAS_PHONE_NUMBER));
                                      if (hasPhone &gt; 0) {
                                          String[] projectionPhone = new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER };
                                          String selectionPhone = ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                                  + " = ?";
                                          String[] selectionArgPhone = new String[] { contactID };
                                          Cursor phoneCursor = cr
                                                  .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                                          projectionPhone,
                                                          selectionPhone,
                                                          selectionArgPhone, null);
  
                                          if (phoneCursor.getCount() &gt; 0) {
                                              while (phoneCursor.moveToNext()) {
                                                  String phone = phoneCursor
                                                          .getString(phoneCursor
                                                                  .getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
                                                  if (phone != null) {
                                                      ContactData tmp = new ContactData(
                                                              name, fName, mName,
                                                              lName, phone, contactID);
                                                      if (raw.size() == 0) {
                                                          raw.add(tmp);
                                                      } else if (!(raw
                                                              .get(raw.size() - 1))
                                                              .getContactID()
                                                              .equalsIgnoreCase(
                                                                      tmp.getContactID())) {
                                                          raw.add(tmp);
                                                      }
                                                  }
                                              }
                                          }
                                          phoneCursor.close();
                                      }
                                  }
                              }
                              common.close();
                          }
                      } while (nameCursor.moveToNext());
                      nameCursor.close();
                  }
              } catch (NumberFormatException e) {
                  e.printStackTrace();
              } catch (IllegalArgumentException e) {
                  e.printStackTrace();
              } catch (Exception e) {
                  e.printStackTrace();
              }
  </code></pre>
  
  <p>I need code for <strong>getPrefContacts()</strong> <em>method</em></p>
  
  <p><strong>2:</strong> How to reduce the query time consumed with respect to above code or any other way to do so??</p>
  
  <p>Hope anybody can help me.</p>
  
  <p>Thanks in advance.</p>