Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort ABAddressBook contact through First Name

I have a sort function that sorts the Address book by Last Name, I need to modify this code so that it will sort by first Name. Where do I need to make change in this code. I know its a simple change but I am unable to figure it out. This is the code that sorts the contacts list by Last Name

ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFMutableArrayRef peopleMutable = CFArrayCreateMutableCopy
                                       (
                                          kCFAllocatorDefault,
                                          CFArrayGetCount(people),
                                          people
                                       );


CFArraySortValues
       (
        peopleMutable,
        CFRangeMake(0, CFArrayGetCount(peopleMutable)),
        (CFComparatorFunction) ABPersonComparePeopleByName,
        (void*) ABPersonGetSortOrdering()
       );
like image 200
Francis F Avatar asked Aug 08 '13 07:08

Francis F


2 Answers

Implement your sort method by bellow put kABPersonSortByFirstName instead of (void*) ABPersonGetSortOrdering():-

CFArraySortValues(peopleMutable,
                  CFRangeMake(0, CFArrayGetCount(peopleMutable)),
                  (CFComparatorFunction) ABPersonComparePeopleByName,
                  kABPersonSortByFirstName);

Credit goes to this

like image 148
Nitin Gohel Avatar answered Nov 02 '22 06:11

Nitin Gohel


Try this

 ABRecordRef source = ABAddressBookCopyDefaultSource(addressBook);
    addressBookArray = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source, kABPersonSortByLastName);

hope it helps you.

like image 40
Toseef Khilji Avatar answered Nov 02 '22 06:11

Toseef Khilji