我按如下方式对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];
但是,我不知道我应该在selector参数中添加什么。谢谢!
答案 0 :(得分:43)
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:str_key
ascending:YES selector:@selector(caseInsensitiveCompare:)];
ads_printers_array = [ads_printers_array sortedArrayUsingDescriptors:[NSArray
arrayWithObject:sortDescriptor]];
答案 1 :(得分:0)
对于Swift,请执行以下操作:
let sortDescriptor = NSSortDescriptor(key: sortDescriptorKey, ascending: true, selector: #selector(NSString.caseInsensitiveCompare(_:)))