NSMutableArray排序 - 不区分大小写

25

我正在对一个 NSMutableArray 进行排序:

    NSSortDescriptor *sortDescriptor;
    sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:str_key ascending:bool_asc_desc] autorelease];
    NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
    NSArray *sortedArray;
    sortedArray = [ads_printers_array sortedArrayUsingDescriptors:sortDescriptors];

问题在于这是大小写敏感的,我想将其变成不区分大小写。我该怎么做?我尝试阅读文档,并找到了类似这样的内容:

sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:str_key ascending:bool_asc_desc selector: @selector(caseInsensitiveCompare)] autorelease];

然而,我不知道在选择器参数中应该放什么。谢谢!


请查看以下帖子:https://dev59.com/4m035IYBdhLWcg3wPNZk#5542888 - Bo.
1
谢谢!对于任何感兴趣的人,我使用的选择器方法是(localizedCaseInsensitiveCompare:)。干杯! - Kevin
1
可能是重复的问题:我想使用NSSortDescriptor对数组进行排序 - Parag Bafna
完美,谢谢。(在 Objective-C 中有什么是直截了当的吗..?!) - Mike Gledhill
2个回答

49
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:str_key 
    ascending:YES selector:@selector(caseInsensitiveCompare:)];

ads_printers_array = [ads_printers_array sortedArrayUsingDescriptors:[NSArray 
    arrayWithObject:sortDescriptor]];

1
对于Swift,请执行以下操作: let sortDescriptor = NSSortDescriptor(key: sortDescriptorKey, ascending: true, selector: #selector(NSString.caseInsensitiveCompare(_:)))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接