iphone - UITableView 与联系人应用程序中的名字姓氏列表

发布于 2024-10-13 04:56:33 字数 634 浏览 4 评论 0原文

我有一个包含一系列联系人的应用程序。数组中的每个对象都是包含名字和姓氏的字典。 在 iPhone 上的联系人应用程序中,它显示用户按字母顺序排序。如果名字不可用,则排序时会考虑姓氏。

我怎样才能进行类似的排序。 我知道如何根据一个字段进行排序,如下所示:

    NSArray* tempArray = [jsonData objectForKey:@"contacts"];
    NSSortDescriptor *sortDescriptor;
    sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"first_name"
                                                 ascending:YES] autorelease];
    NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
    contactsArray = [[NSArray alloc] initWithArray:[tempArray sortedArrayUsingDescriptors:sortDescriptors]];

如何根据名字或姓氏可用性对联系人数组进行排序。

I've an application which has an array of contacts. Each object in the array is dictionary which holds first name and last name.
In the contacts application on iphone, it shows the users sorting alphabetically. If first name is not available last name is consider while sorting.

How can I do similar sorting.
I know how to do sorting based on one field as following:

    NSArray* tempArray = [jsonData objectForKey:@"contacts"];
    NSSortDescriptor *sortDescriptor;
    sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"first_name"
                                                 ascending:YES] autorelease];
    NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
    contactsArray = [[NSArray alloc] initWithArray:[tempArray sortedArrayUsingDescriptors:sortDescriptors]];

How can I sort the array of contacts based on first name or last name availability.

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

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

发布评论

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

评论(1

尝蛊 2024-10-20 04:56:34

供您参考:

{
    ...
    NSInteger order = (NSInteger)ABPersonGetSortOrdering();
        if (order == kABPersonSortByFirstName) 
        {
            int firstNameFirst = YES;
            [self.contactList sortUsingFunction:alphabeticPersonNameSort context:&firstNameFirst];
        }
        else 
        {
            int firstNameFirst = NO;
            [self.contactList sortUsingFunction:alphabeticPersonNameSort context:&firstNameFirst];
        }
    ...
}

static NSInteger alphabeticPersonNameSort(NSArray *obj1, NSArray *obj2, void *firstNameFirst)
{
    if ((*(int *) firstNameFirst) == NO)
    {
        // FullName = LastName, FirstName
            // FirstName: [obj1 objectAtIndex:0]
            // LastName: [obj1 objectAtIndex:1]
        NSString *fullName1 = [NSString stringWithFormat:@"%@, @%", [obj1 objectAtIndex:1], [obj1 objectAtIndex:0]];
        NSString *fullName2 = [NSString stringWithFormat:@"%@, @%", [obj2 objectAtIndex:1], [obj2 objectAtIndex:0]];
        return [fullName1 localizedCaseInsensitiveCompare:fullName2];
    }
    else
    {
        // Only sorted by using firstName

        NSString *fullName1 = [NSString stringWithFormat:@"%@", [obj1 objectAtIndex:0]];
        NSString *fullName2 = [NSString stringWithFormat:@"%@", [obj2 objectAtIndex:0]];
        return [fullName1 localizedCaseInsensitiveCompare:fullName2];
    }
}

For your reference:

{
    ...
    NSInteger order = (NSInteger)ABPersonGetSortOrdering();
        if (order == kABPersonSortByFirstName) 
        {
            int firstNameFirst = YES;
            [self.contactList sortUsingFunction:alphabeticPersonNameSort context:&firstNameFirst];
        }
        else 
        {
            int firstNameFirst = NO;
            [self.contactList sortUsingFunction:alphabeticPersonNameSort context:&firstNameFirst];
        }
    ...
}

static NSInteger alphabeticPersonNameSort(NSArray *obj1, NSArray *obj2, void *firstNameFirst)
{
    if ((*(int *) firstNameFirst) == NO)
    {
        // FullName = LastName, FirstName
            // FirstName: [obj1 objectAtIndex:0]
            // LastName: [obj1 objectAtIndex:1]
        NSString *fullName1 = [NSString stringWithFormat:@"%@, @%", [obj1 objectAtIndex:1], [obj1 objectAtIndex:0]];
        NSString *fullName2 = [NSString stringWithFormat:@"%@, @%", [obj2 objectAtIndex:1], [obj2 objectAtIndex:0]];
        return [fullName1 localizedCaseInsensitiveCompare:fullName2];
    }
    else
    {
        // Only sorted by using firstName

        NSString *fullName1 = [NSString stringWithFormat:@"%@", [obj1 objectAtIndex:0]];
        NSString *fullName2 = [NSString stringWithFormat:@"%@", [obj2 objectAtIndex:0]];
        return [fullName1 localizedCaseInsensitiveCompare:fullName2];
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文