Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Jon-Schneider/6320587 to your computer and use it in GitHub Desktop.
Save Jon-Schneider/6320587 to your computer and use it in GitHub Desktop.
A bug in the iOS Address Book library returns contacts in the wrong alphabetical sort order when requesting them by First or Last name. I wrote this code as a way to do my own sort of all contacts. This method will sort by first or last name based on an NSUserDefault bool, falling back onto their company name if the contact has no first or last …
#pragma mark - All Contacts list sort handling function and method.
CFComparisonResult ABPersonComparePeopleByIdentifier (ABRecordRef person1, ABRecordRef person2, ABPersonSortOrdering ordering) {
NSString *firstName1 = (__bridge NSString *)ABRecordCopyValue(person1, kABPersonFirstNameProperty);
NSString *lastName1 = (__bridge NSString *)ABRecordCopyValue(person1, kABPersonLastNameProperty);
NSString *orgName1 = (__bridge NSString *)ABRecordCopyValue(person1, kABPersonOrganizationProperty);
NSString *identifier1;
BOOL firstNameSortingIsOn = [[NSUserDefaults standardUserDefaults] boolForKey:@"first-name-sort"];
NSString *firstName2 = (__bridge NSString *)ABRecordCopyValue(person2, kABPersonFirstNameProperty);
NSString *lastName2 = (__bridge NSString *)ABRecordCopyValue(person2, kABPersonLastNameProperty);
NSString *orgName2 = (__bridge NSString *)ABRecordCopyValue(person2, kABPersonOrganizationProperty);
NSString *identifier2;
if (!firstNameSortingIsOn) {
identifier1 = (lastName1) ? lastName1 : firstName1;
identifier1 = (identifier1) ? identifier1 : orgName1;
identifier2 = (lastName2) ? lastName2 : firstName2;
identifier2 = (identifier2) ? identifier2 : orgName2;
} else { //First name sorting is on
identifier1 = (firstName1) ? firstName1 : lastName1;
identifier1 = (identifier1) ? identifier1 : orgName1;
identifier2 = (firstName2) ? firstName2 : lastName2;
identifier2 = (identifier2) ? identifier2 : orgName2;
}
if (!identifier1 && !identifier2) {
return kCFCompareEqualTo;
}
else if (!identifier1 || !identifier2) {
return (identifier1) ? kCFCompareGreaterThan : kCFCompareLessThan;
}
return [identifier1 caseInsensitiveCompare:identifier2];
//This would be the function used to compare CFStrings rather than NSStrings.
//return CFStringCompare(identifier1, identifier2, 1);
/*
kCFCompareLessThan when person1 goes before person2.
kCFCompareEqualTo when person1 and person2 have the same name.
kCFCompareGreaterThan when person1 goes after person2.
*/
}
-(CFMutableArrayRef)getSortedAllContactArray
{
CFArrayRef peopleRefs = ABAddressBookCopyArrayOfAllPeople(_addressBook);
CFMutableArrayRef peopleMutable = CFArrayCreateMutableCopy(kCFAllocatorDefault, CFArrayGetCount(peopleRefs), peopleRefs);
CFArraySortValues(peopleMutable, CFRangeMake(0, CFArrayGetCount(peopleMutable)), (CFComparatorFunction) ABPersonComparePeopleByIdentifier, NULL);
CFRelease(peopleRefs);
return peopleMutable;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment