5

我有一个按姓氏对通讯簿进行排序的排序功能,我需要修改此代码,使其按名字排序。我需要在哪里更改此代码。我知道这是一个简单的更改,但我无法弄清楚。这是按姓氏对联系人列表进行排序的代码

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

4 回答 4

8

通过以下方式实现您的排序方法,kABPersonSortByFirstName而不是 (void*) ABPersonGetSortOrdering():-

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

归功于

于 2013-08-08T07:07:27.173 回答
1

尝试这个

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

希望它可以帮助你。

于 2013-08-08T07:07:16.693 回答
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]];
        }

希望它对你有用。

于 2013-08-08T07:18:23.130 回答
0

这对我有用:

 //Sorting

 ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
            ABRecordRef source = ABAddressBookCopyDefaultSource(addressBook);
            NSArray *allPeople = (__bridge NSArray *)ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source, kABPersonSortByFirstName);
于 2015-10-08T05:39:06.913 回答