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()
);
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
Try this
ABRecordRef source = ABAddressBookCopyDefaultSource(addressBook);
addressBookArray = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source, kABPersonSortByLastName);
hope it helps you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With