简体   繁体   English

iPhone-UITableView,具有联系人应用程序中的姓氏列表

[英]iphone - UITableView with list of firstname lastname as in contacts application

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. 在iPhone的通讯录应用程序中,它显示用户按字母顺序排序。 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. 如何根据名字或姓氏的可用性对联系人数组进行排序。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM