24

我排序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];

但是,我不知道应该在选择器参数中添加什么。谢谢!

4

2 回答 2

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

ads_printers_array = [ads_printers_array sortedArrayUsingDescriptors:[NSArray 
    arrayWithObject:sortDescriptor]];
于 2013-04-22T13:23:14.543 回答
1

对于 Swift,请执行以下操作:

let sortDescriptor = NSSortDescriptor(key: sortDescriptorKey, ascending: true, selector: #selector(NSString.caseInsensitiveCompare(_:)))

于 2017-10-03T22:25:35.057 回答