如何通过名字对ABAddressBook联系人进行排序

时间:2013-08-08 07:02:25

标签: ios objective-c sorting abaddressbook

我有一个排序函数,按姓氏对地址簿进行排序,我需要修改此代码,以便按名字排序。我在哪里需要对此代码进行更改。我知道这是一个简单的改变,但我无法弄清楚。这是按姓氏对联系人列表进行排序的代码

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


CFArraySortValues
       (
        peopleMutable,
        CFRangeMake(0, CFArrayGetCount(peopleMutable)),
        (CFComparatorFunction) ABPersonComparePeopleByName,
        (void*) ABPersonGetSortOrdering()
       );

4 个答案:

答案 0 :(得分:8)

通过下面的kABPersonSortByFirstName代替(void*) ABPersonGetSortOrdering()来实施排序方法: -

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

信用转到this

答案 1 :(得分:1)

试试这个

 ABRecordRef source = ABAddressBookCopyDefaultSource(addressBook);
    addressBookArray = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source, kABPersonSortByLastName);
希望它可以帮到你。

答案 2 :(得分:0)

您可以使用此代码对它们进行排序,如果您想使用相同的代码,请检查Nitin的答案: -

        ABRecordRef person = (__bridge ABRecordRef)record;

        NSString *firstName = (__bridge_transfer NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);

        if ( [beginsPredicate evaluateWithObject:firstName] )
        {
            ABRecordID abRecordID = ABRecordGetRecordID(person);

            [self.filteredPeople addObject:[NSNumber numberWithInt:abRecordID]];
        }

希望它对你有用。

答案 3 :(得分:0)

这对我有用:

 //Sorting

 ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
            ABRecordRef source = ABAddressBookCopyDefaultSource(addressBook);
            NSArray *allPeople = (__bridge NSArray *)ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source, kABPersonSortByFirstName);