【问题标题】:Sorting NSMutableArray with NSStrings in alphabetical order使用 NSStrings 按字母顺序对 NSMutableArray 进行排序
【发布时间】:2011-04-07 16:29:01
【问题描述】:

我有一个带有 NSStrings 的 NSMutableArray,我想按字母顺序对它们进行排序,看起来并不难,但我没有找到好的解决方案。请帮帮我。

【问题讨论】:

标签: sorting nsmutablearray alphabetical


【解决方案1】:

使用sortUsingSelector: 方法和compare: 作为比较选择器:

NSMutableArray* strings = [NSMutableArray arrayWithObjects:@"dog", @"ant", @"cat",nil];
[strings sortUsingSelector:@selector(compare:)];
NSLog(@"%@", strings);

【讨论】:

  • @selector(caseInsensitiveCompare:) 会比@selector(compare:) 更正确,IMO。此解决方案将["Dog", "cat", "Ant"] 排序为["Ant", "Dog", "cat"],这既不是技术上的字母顺序,也不是大多数用例所需的结果。
  • 更好的是,使用 @selector(localizedCaseInsensitiveCompare:) 来设置你,以防你想支持本地化。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-12
  • 2011-08-29
  • 2017-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多