how to sort iphone contact book?

后端 未结 3 377
囚心锁ツ
囚心锁ツ 2021-02-01 08:10

How can i sort (or retrieve sorted array of ) an iphone contact book by first name & last name programmatically ??

Any help will be well appreciated...! Thanks

3条回答
  •  有刺的猬
    2021-02-01 09:03

    Call ABAddressBookCopyArrayOfAllPeople() to get an array of all person records in the address book. Then follow the documentation:

    To sort an array of people, use the function CFArraySortValues with the function ABPersonComparePeopleByName as the comparator and a context of the type ABPersonSortOrdering. The user’s desired sort order, as returned by ABPersonGetSortOrdering, is generally the preferred context.

    The following code listing shows an example of sorting the entire Address Book database:

    ABAddressBookRef addressBook = ABAddressBookCreate();
    CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook);
    CFMutableArrayRef peopleMutable = CFArrayCreateMutableCopy(
                                              kCFAllocatorDefault,
                                              CFArrayGetCount(people),
                                              people
                                      );
    
     CFArraySortValues(
            peopleMutable,
            CFRangeMake(0, CFArrayGetCount(peopleMutable)),
            (CFComparatorFunction) ABPersonComparePeopleByName,
            (void*) ABPersonGetSortOrdering()
    ); 
    
    CFRelease(addressBook);
    CFRelease(people);
    CFRelease(peopleMutable);
    

提交回复
热议问题