NSSortDescriptor具有名字和姓氏,还是名字或姓氏?

时间:2022-05-13 08:22:24

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.

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

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 个解决方案

#1


1  

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

如果您有自定义条件,则可以使用比较器块而不是描述符。例如,您可以使用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.

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

#1


1  

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

如果您有自定义条件,则可以使用比较器块而不是描述符。例如,您可以使用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.

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