【问题标题】:Sorting array with NSSortDescriptor for specific item使用 NSSortDescriptor 对特定项目的数组进行排序
【发布时间】:2013-08-28 17:54:13
【问题描述】:

我正在使用NSSortDescriptor 对数组进行如下排序:

NSSortDescriptor *titleDescriptor = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObjects: titleDescriptor, nil]; 

self.items = (NSMutableArray*)[self.items sortedArrayUsingDescriptors:sortDescriptors];

效果很好。我想要做的是始终将一个特定的“标题”(从中我知道确切的字符串)放在列表​​的顶部,然后让NSSortDescriptor 将其余标题按字母顺序排列在所述标题下方。使用NSSortDescriptor 完成此操作的语法是什么?

【问题讨论】:

    标签: iphone ios cocoa-touch nsarray nssortdescriptor


    【解决方案1】:

    您应该使用sortedArrayUsingSelector: 等其他方法之一来获得这种级别的控制。

    此外,不要只将结果转换为可变数组。如果数组是可变的,请使用对可变数组进行排序的方法。如果您使用该方法创建一个新数组,请通过在结果上调用mutableCopy 来确保它是可变的。

    【讨论】:

      猜你喜欢
      • 2013-03-10
      • 2012-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-27
      • 1970-01-01
      • 2010-10-11
      相关资源
      最近更新 更多