【问题标题】:NSMutableArray sorting - case insensitiveNSMutableArray 排序 - 不区分大小写
【发布时间】:2012-06-22 07:05:00
【问题描述】:

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

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

【问题讨论】:

标签: objective-c cocoa nssortdescriptor


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:str_key 
        ascending:YES selector:@selector(caseInsensitiveCompare:)];
    
    ads_printers_array = [ads_printers_array sortedArrayUsingDescriptors:[NSArray 
        arrayWithObject:sortDescriptor]];
    

    【讨论】:

      猜你喜欢
      • 2019-01-31
      • 2020-09-13
      • 1970-01-01
      • 2014-11-10
      • 2015-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多