Search code examples
androidcursorandroid-contacts

How to get contacts sorted by phone number?


Had tried

Cursor c = getContentResolver().query(Data.CONTENT_URI, null, null, null, Phone.NUMBER+" ASC");

But apparently the contacts are not sorted correctly.


Solution

  • Ok, I now understand how this works - it is actually ready sorted.

    By this:

    getContentResolver().query(Data.CONTENT_URI, null, null, null, Phone.NUMBER+" ASC");
    

    I get:

    id: 60, data1: +34600000000, mimetype: phone_v2
    id: 59, data1: +34698765432, mimetype: phone_v2
    id: 59, data1: JohnDavid1 Doe, mimetype: name
    id: 60, data1: JohnDavid2 Doe, mimetype: name
    

    If I pass DESC to query(), will get:

    id: 60, data1: JohnDavid2 Doe, mimetype: name
    id: 59, data1: JohnDavid1 Doe, mimetype: name
    id: 59, data1: +34698765432, mimetype: phone_v2
    id: 60, data1: +34600000000, mimetype: phone_v2
    

    So result is actually sorted, though it is not sorting Contact ID as I expected.