Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9046327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T11:38:36+00:00 2026-06-16T11:38:36+00:00

I have this code below that I managed to get the names & phone

  • 0

I have this code below that I managed to get the names & phone numbers listed out from the address book, but how do I sort it by the first name?

ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);
abContactArray = (__bridge NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBookRef); // get address book contact array

NSInteger totalContacts =[abContactArray count];

for(NSUInteger loop= 0 ; loop < totalContacts; loop++)
{
    ABRecordRef record = (__bridge ABRecordRef)[abContactArray objectAtIndex:loop]; // get address book record

    if(ABRecordGetRecordType(record) ==  kABPersonType) // this check execute if it is person group
    {
        //ABRecordID recordId = ABRecordGetRecordID(record); // get record id from address book record

        //NSString *recordIdString = [NSString stringWithFormat:@"%d",recordId]; // get record id string from record id
        //NSLog(@"Record: %@", recordIdString);

        NSString *firstNameString = (__bridge NSString*)ABRecordCopyValue(record,kABPersonFirstNameProperty); // fetch contact first name from address book

        NSString *lastNameString = (__bridge NSString*)ABRecordCopyValue(record,kABPersonLastNameProperty); // fetch contact last name from address book
                                                                                                            //NSString *contactEmail = (__bridge NSString*)ABRecordCopyValue(record,kABPersonEmailProperty); // fetch contact last name from address book

        NSString * fullName = [NSString stringWithFormat:@"%@ %@", firstNameString, lastNameString];

        [name addObject: fullName];

        ABMultiValueRef phoneNumberMultiValue = ABRecordCopyValue(record, kABPersonPhoneProperty);
        NSUInteger phoneNumberIndex;
        for (phoneNumberIndex = 0; phoneNumberIndex < ABMultiValueGetCount(phoneNumberMultiValue); phoneNumberIndex++) {

            CFStringRef labelStingRef = ABMultiValueCopyLabelAtIndex (phoneNumberMultiValue, phoneNumberIndex);

            //NSString *phoneLabelLocalized = (__bridge NSString*)ABAddressBookCopyLocalizedLabel(labelStingRef);

            phoneNumber  = (__bridge NSString *)ABMultiValueCopyValueAtIndex(phoneNumberMultiValue, phoneNumberIndex);

            CFRelease(labelStingRef);

            //NSLog(@"Name: %@ %@: %@ | %@", firstNameString, lastNameString, phoneNumber, emailAddresses);

        }
        [phone addObject: phoneNumber];


    }
}

I tried putting in these codes:

ABRecordRef record = (__bridge ABRecordRef)[abContactArray objectAtIndex:loop]; // get address book record

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


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

//NSMutableArray *data = [(__bridge NSArray *) peopleMutable mutableCopy];
NSMutableArray* data = [NSMutableArray arrayWithArray: (__bridge NSArray*) peopleMutable];

NSLog(@"sort: %@", data);

But the nslog gave me these output:

sort: (
"<CPRecord: 0xaa5d250 ABPerson>",
"<CPRecord: 0xaa6e050 ABPerson>",
"<CPRecord: 0xaa3d7d0 ABPerson>",
"<CPRecord: 0xaa515d0 ABPerson>",
"<CPRecord: 0xaa43b90 ABPerson>",
"<CPRecord: 0xaa6b780 ABPerson>"
)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T11:38:37+00:00Added an answer on June 16, 2026 at 11:38 am

    You can sort the entries by name using:

    CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook);
    CFMutableArrayRef peopleMutable = CFArrayCreateMutableCopy(kCFAllocatorDefault,
                                                               CFArrayGetCount(people),
                                                               people);
    
    CFArraySortValues(peopleMutable,
                      CFRangeMake(0, CFArrayGetCount(peopleMutable)),
                      (CFComparatorFunction) ABPersonComparePeopleByName,
                      kABPersonSortByFirstName);
    
    // or to sort by the address book's choosen sorting technique
    //
    // CFArraySortValues(peopleMutable,
    //                   CFRangeMake(0, CFArrayGetCount(peopleMutable)),
    //                   (CFComparatorFunction) ABPersonComparePeopleByName,
    //                   (void*) ABPersonGetSortOrdering());
    
    CFRelease(people);
    
    // If you don't want to have to go through this ABRecordCopyValue logic
    // in the rest of your app, rather than iterating through doing NSLog,
    // build a new array as you iterate through the records.
    
    for (CFIndex i = 0; i < CFArrayGetCount(peopleMutable); i++)
    {
        ABRecordRef record = CFArrayGetValueAtIndex(peopleMutable, i);
        NSString *firstName = CFBridgingRelease(ABRecordCopyValue(record, kABPersonFirstNameProperty));
        NSString *lastName = CFBridgingRelease(ABRecordCopyValue(record, kABPersonLastNameProperty));
        NSLog(@"person = %@, %@", lastName, firstName);
    }
    
    CFRelease(peopleMutable);
    

    Or you could use this technique:

    NSArray *originalArray = CFBridgingRelease(ABAddressBookCopyArrayOfAllPeople(addressBook));
    abContactArray = [originalArray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        ABRecordRef record1 = (__bridge ABRecordRef)obj1; // get address book record
        NSString *firstName1 = CFBridgingRelease(ABRecordCopyValue(record1, kABPersonFirstNameProperty));
        NSString *lastName1 = CFBridgingRelease(ABRecordCopyValue(record1, kABPersonLastNameProperty));
    
        ABRecordRef record2 = (__bridge ABRecordRef)obj2; // get address book record
        NSString *firstName2 = CFBridgingRelease(ABRecordCopyValue(record2, kABPersonFirstNameProperty));
        NSString *lastName2 = CFBridgingRelease(ABRecordCopyValue(record2, kABPersonLastNameProperty));
    
        NSComparisonResult result = [firstName1 compare:firstName2];
        if (result != NSOrderedSame)
            return result;
        else
            return [lastName1 compare:lastName2];
    }];
    
    for (id object in abContactArray)
    {
        ABRecordRef record = (__bridge ABRecordRef)object; // get address book record
        NSString *firstName = CFBridgingRelease(ABRecordCopyValue(record, kABPersonFirstNameProperty));
        NSString *lastName = CFBridgingRelease(ABRecordCopyValue(record, kABPersonLastNameProperty));
        NSLog(@"person = %@, %@", lastName, firstName);
    }
    

    The former seems cleaner, but just another option, in case you want to avoid CFRelease in an ARC world.

    By the way, use ABAddressBookRequestAccessWithCompletion in iOS 6 to check to make sure you have permission to access the address book (with a conditional check to make sure it still works with earlier versions of iOS). And even if you’re on earlier versions of iOS, you should be asking the user for there permission manually.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this javascript code below that uses jquery, it is suppoed to be
I have a data that looks like this . And my code below simply
I have a code like this below, which gives me a $link that equals
I have this code below. I need to use a class id instead of
I have this code below. As you can see I am passing two variables
I have this code below: $insert = array(); for ($i = 1, $n =
I have this below code and it work fine header (content-type: text/xml); $xml =
I have this snippet of code below. I want to pass the value of
I have this kind of code below, how can I bind the visibility of
I have a problem with this code: (The error is below the code) public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.