在 iPhone 上按字母顺序对 ABRecord 进行排序

发布于 2024-10-29 08:08:02 字数 631 浏览 3 评论 0原文

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

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:) 但我不知道它是如何工作的。

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(compare:) but I have no idea how that is supposed to work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苏大泽ㄣ 2024-11-05 08:08:02
NSSortDescriptor *mySorter = [[NSSortDescriptor alloc] initWithKey:@"lastName" ascending:YES];
[contacts sortUsingDescriptors:[NSArray arrayWithObject:mySorter]];
[mySorter release];
NSSortDescriptor *mySorter = [[NSSortDescriptor alloc] initWithKey:@"lastName" ascending:YES];
[contacts sortUsingDescriptors:[NSArray arrayWithObject:mySorter]];
[mySorter release];
稚然 2024-11-05 08:08:02

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

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

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

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]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文