Search code examples
androidandroid-contactsandroid-contentresolvercontactscontract

Android: Show all contacts sorted by favorites


I know how to fetch all contacts as well as how to fetch the favorited contacts. Is there a way to combine the two and sort by favorites?

getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, "starred=?", new String[] {"1"}, <sort by favorites?>);

Solution

  • Try to use this query to get all contacts order by favorites and then by display name.

    getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
    null, 
    null, 
    null,
    ContactsContract.Contacts.STARRED + " DESC, " + ContactsContract.Contacts.DISPLAY_NAME_PRIMARY + " ASC");