【问题标题】:NSArray sorting issue with acronyms being sorted firstNSArray 排序问题,首字母缩写词首先排序
【发布时间】:2012-02-03 20:33:00
【问题描述】:

我有一个 NSArray,我想按字母顺序排序,我目前正在使用 sortedArrayUsingSelector:@selector (localizedCaseInsensitiveCompare:)。这返回

CAA
CBL
CCC
Cab
Car
Cat

首字母缩略词按字母顺序排列,然后是其他缩写词。我将使用什么来排序,以便首字母缩写词与常规单词一起排序?在 Apple 的文档中似乎找不到答案。

book = [[self.sections valueForKey:[[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];

【问题讨论】:

    标签: objective-c ios cocoa-touch nsarray


    【解决方案1】:

    你可以使用

    - (NSArray *)sortedArrayUsingComparator:(NSComparator)cmptr
    

    并为所需的行为创建您的 NSComparator。

    像这样:

    NSarray *sortedArray = [myArray sortedArrayUsingComparator:^(id obj1, id obj2) {
      //Decide in the order
      if (yourCondition)
      {
      return NSOrderedSame
      }
    }];
    

    【讨论】:

    • 谢谢奥斯卡。你能详细说明一下吗?我的完整代码是book = [[self.sections valueForKey:[[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
    • 感谢您编辑您的答案...我从未使用过 NSComparator 所以我需要解决这个问题,看看它是否有效。我会告诉你。我很惊讶为什么首先捆绑首字母缩略词......
    猜你喜欢
    • 2013-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多