NSSortDescriptor with first name and last name,or with either first name or last name?

余生颓废 提交于 2019-12-07 20:25:19

问题


I am doing sorting of an contact info array,it's working fine when I entered first name and last for particular contact,but it's not working fine when any of it is missing,

For Ex: if I enter first name John and last name mickey ,then it gives proper sorting,but if I enter only mickey then it comes at last in # section in UITableview.,so what I need to do here in this type of case.

My code is as below,

NSSortDescriptor *sortDescriptorFirstName = [[[NSSortDescriptor alloc] initWithKey:@"firstName" ascending:YES selector:@selector(localizedStandardCompare:)] autorelease];

NSSortDescriptor *sortDescriptorLastName = [[[NSSortDescriptor alloc] initWithKey:@"lastName" ascending:YES selector:@selector(localizedStandardCompare:)] autorelease];

NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptorFirstName,sortDescriptorLastName,nil];

if(favFlag){
    favContacts = [[contactsData sortedArrayUsingDescriptors:sortDescriptors] mutableCopy];
    [self setListContent:favContacts];
}
else {
    contacts = [[contactsData sortedArrayUsingDescriptors:sortDescriptors] mutableCopy];
    [self setListContent:contacts];
}

[contactTableView reloadData];

回答1:


If you have custom criteria, you can use comparator blocks instead of descriptors. For example, you can use sortedArrayUsingComparator:

contacts = [contactsData sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
    NSString *sortName1 = [self sortNameWithFirst:[obj1 objectForKey:@"firstName"]
                                             last:[obj1 objectForKey:@"lastName"]];

    NSString *sortName2 = [self sortNameWithFirst:[obj2 objectForKey:@"firstName"]
                                             last:[obj2 objectForKey:@"lastName"]];

    return [sortName1 caseInsensitiveCompare:sortName2];
}];

Where I have this little utility method to make it easier to create the "sort name", my term for the string by which I'm ultimately going to sort:

- (NSString *)sortNameWithFirst:(NSString *)firstName last:(NSString *)lastName
{
    if (firstName && lastName)
        return [NSString stringWithFormat:@"%@ %@", firstName, lastName];
    if (firstName)
        return firstName;
    if (lastName)
        return lastName;
    return nil;
}

This code takes this list:

NSArray *contactsData = @[
    @{@"firstName":@"Rob", @"lastName":@"Zimmer"},
    @{@"firstName":@"Sting"},
    @{@"firstName":@"Rob", @"lastName":@"Ryan"},
    @{@"firstName":@"Cher"},
    @{@"lastName":@"Smith"}
];

And gives me "Cher", "Rob Ryan", "Rob Zimmer", "Smith", and "Sting".

To be honest, I wasn't entirely clear how your wanted to sort it on the basis of your question, but you get the idea. The comparator block of sortedArrayUsingComparator gives you the ability to create whatever custom sort criteria you want. It just needs to return one of the NSComparisonResult values of either NSOrderedAscending, NSOrderedSame, or NSOrderedDescending (which I'm conveniently using caseInsensitiveCompare to generate for me). For a list of searching alternatives see the Sorting methods listed in the NSArray Class Reference.



来源:https://stackoverflow.com/questions/13873077/nssortdescriptor-with-first-name-and-last-name-or-with-either-first-name-or-last

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!