ios - 按字典中键的值对字典的 NSArray 进行排序

我有一个由字典填充的数组,我需要按字典的其中一个键的值按字母顺序对数组进行排序。

这是我的数组:

tu dictus: (
    {
    brand = Ryul;
    productTitle = Any;
    quantity = 1;
    subBrand = "Ryul INJ";
    type = Product;
},
    {
    brand = Trol;
    productTitle = Different;
    quantity = 2;
    subBrand = "";
    type = Brand;
},
    {
    brand = Dtor;
    productTitle = Any;
    quantity = 1;
    subBrand = "";
    type = Product;
},
    {
    brand = Ryul;
    productTitle = Different;
    quantity = 2;
    subBrand = "Ryul CHES";
    type = SubBrand;
},
    {
    brand = Anan;
    productTitle = Any;
    quantity = 1;
    subBrand = "";
    type = Product;
}
)

通常我会使用对数组进行排序

myArray = [uniqueProdsArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

但是如何使用字典的brand键进行排序呢?

最佳答案

我认为这样做可以:

brandDescriptor = [[NSSortDescriptor alloc] initWithKey:@"brand" ascending:YES];
sortDescriptors = [NSArray arrayWithObject:brandDescriptor];
sortedArray = [myArray sortedArrayUsingDescriptors:sortDescriptors];

我从 Sort Descriptor Programming Topics 中提取了代码.另外,Key-Value Coding sortedArrayUsingDescriptors: 将向 myArray 中的每个元素发送一个 valueForKey:,然后使用标准比较器对返回的值进行排序.

https://stackoverflow.com/questions/9509138/

相关文章:

") 运算符在 C 与 Objec">objective-c - 点 (".") 运算符和箭头 ("->") 运算符在 C 与 Objec

objective-c - objective-c : convert a NSMutableStr

ios - MKMapView:自定义 View 代替 Annotation Pin

objective-c - 将参数传递给由 NSTimer 调用的方法

ios - 如何为 iOS 构建 Unity3d 插件

iphone - NSNumber 和 NSInteger 有什么区别?

iPhone - dequeueReusableCellWithIdentifier 用法

objective-c - Objective-C 中 typeof、__typeof 和 __ty

objective-c - 将自定义 subview (在 xib 中创建)添加到 View Con

c++ - 我可以在同一个 Xcode 项目中拥有 Swift、Objective-C、C 和 C+