How to sort ABAddressBook contact through First Name
Asked Answered
R

4

5

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()
       );
Rather answered 8/8, 2013 at 7:2 Comment(0)
W
8

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

Wanderlust answered 8/8, 2013 at 7:7 Comment(1)
Thanks that seems to work, I wonder why didnt I find that page when I searched for it in stack overflow before and also I did tried that with kABPersonSortByLastName and it showed error so I didnt bother trying the other one:PRather
D
1

Try this

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

hope it helps you.

Durban answered 8/8, 2013 at 7:7 Comment(0)
B
0

you can use this code to sort them and if you want to work with the same code check Nitin's answer:-

        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]];
        }

Hope it will work for you.

Broadbent answered 8/8, 2013 at 7:18 Comment(0)
A
0

This worked for me:

 //Sorting

 ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
            ABRecordRef source = ABAddressBookCopyDefaultSource(addressBook);
            NSArray *allPeople = (__bridge NSArray *)ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source, kABPersonSortByFirstName);
Adherence answered 8/10, 2015 at 5:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.