1

我正在使用以下代码检索联系人姓名:

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

有谁知道按字母顺序排列此列表的方法?我已经阅读过,sortedArrayUsingSelector:@selector(compare:)但我不知道它应该如何工作。

4

2 回答 2

2
NSSortDescriptor *mySorter = [[NSSortDescriptor alloc] initWithKey:@"lastName" ascending:YES];
[contacts sortUsingDescriptors:[NSArray arrayWithObject:mySorter]];
[mySorter release];
于 2011-04-02T22:25:11.953 回答
1

此方法将让您尊重用户按名字或姓氏排序的偏好。

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

专业提示:粗体字表示您要排序的名称部分;否则,当您混合使用[没有名字,没有姓氏,名字和姓氏]的联系人时,它会变得混乱

于 2016-08-17T07:23:59.937 回答