NSSortDescriptor具有名字和姓氏,还是名字或姓氏? [英] NSSortDescriptor with first name and last name,or with either first name or last name?

查看:144
本文介绍了NSSortDescriptor具有名字和姓氏,还是名字或姓氏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在对联系信息数组进行排序,当我输入第一个名字并且最后一个用于特定联系时它工作正常,但是当它缺少任何一个时它不能正常工作,

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,

对于Ex:如果我输入名字John和姓氏mickey,那么它会给出正确的排序,但是如果我只输入mickey,那么它最后会出现在UITableview的#部分中。所以我需要在这里做什么这种情况。

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.

我的代码如下,

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


推荐答案

如果你有自定义标准,你可以使用比较器块而不是描述符。例如,您可以使用 sortedArrayUsingComparator

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

此代码采用以下列表:

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

给我Cher,Rob Ryan,Rob Zimmer,Smith,和Sting。

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

老实说,我并不完全清楚你想要根据你的问题对它进行排序,但你明白了。 sortedArrayUsingComparator 的比较器块使您能够创建所需的任何自定义排序条件。它只需要返回一个 NSComparisonResult NSOrderedAscending NSOrderedSame ,或 NSOrderedDescending (我方便地使用 caseInsensitiveCompare 为我生成)。有关搜索备选方案的列表,请参阅排序 NSArray类参考中列出的方法。

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.

这篇关于NSSortDescriptor具有名字和姓氏,还是名字或姓氏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆