开发者

Sorting ABRecords alphabetically on iPhone

I'm retrieving contact names with this code:

for( int i = 0 ; i < n ; i++ )
{
    Contact *c = [[Contact alloc] init];

    ABRecordRef ref = CFArrayGetValueAtIndex(all, i);
    NSString *firstName = (NSString *)ABRecordCopyValue(ref, kABPersonFirstNameProperty);
    NSString *lastName = (NSString *)ABRecordCopyValue(ref, kABPersonLastNameProperty);
    c.firstName = firstName; //[NSString stringWithFormat:@"%@ %@", firstName, lastName];
    c.lastName = lastName;

    [contacts addObject:c];

    [c release];
}

Does anyone know a way of ordering this list alphabetically? I've read about sortedArrayUsingSelector:@selector(co开发者_开发问答mpare:) but I have no idea how that is supposed to work.


NSSortDescriptor *mySorter = [[NSSortDescriptor alloc] initWithKey:@"lastName" ascending:YES];
[contacts sortUsingDescriptors:[NSArray arrayWithObject:mySorter]];
[mySorter release];


This method will let you respect the user's preferences for sorting by first or last name.

contacts = (bridgedPeople as [ABRecord]).sort {
    (person1, person2) -> Bool in
    return .CompareLessThan == ABPersonComparePeopleByName(person1, person2, ABPersonGetSortOrdering())
}

Pro-tip: Boldface the part of the name that you're sorting on; otherwise it gets confusing when you mix contacts who have [no first name, no last name, first and last name]

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜