如何在ios中排序字典数组

时间:2015-01-09 07:51:45

标签: ios sorting nsarray nsdictionary nssortdescriptor

我有这样的dict formate。我需要在字典的dict键中使用“quantity1”键对数组进行排序。

array: (
{
  brand = Ryul;
  productTitle = Any;
  quantity = 1;
  subBrand = "Ryul INJ";
  type = Product;
  dict = {
         brand1 = Trol;
         productTitle1 = Different;
         quantity1 = 2;
         subBrand1 = "";
         type1 = Brand;
      };
},
{
  brand = Trol;
  productTitle = Different;
  quantity = 2;
  subBrand = "";
  type = Brand;
  dict = {
         brand1 = Trol;
         productTitle1 = Different;
         quantity1 = 2;
         subBrand1 = "";
         type1 = Brand;
      };
 }
)

我使用此代码进行排序:

    NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"quantity"  ascending:YES];
    stories=[stories sortedArrayUsingDescriptors:[NSArray arrayWithObjects:descriptor,nil]];
   recent = [stories copy];

当我使用键“数量”时,这是有效的。但是当我使用密钥“quantity1”时,它无法正常工作。

我将如何排序?帮助我

1 个答案:

答案 0 :(得分:11)

使用

NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"dict.quantity1"  ascending:YES];
stories=[stories sortedArrayUsingDescriptors:[NSArray arrayWithObjects:descriptor,nil]];
关键是dict。数量1