【问题标题】:how to sort array of dictionaries in ios如何在ios中对字典数组进行排序
【发布时间】:2015-03-07 12:10:58
【问题描述】:

我有这样的 dict 格式。我需要使用字典的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”时它不起作用。

我将如何排序?帮帮我

【问题讨论】:

    标签: ios sorting nsarray nsdictionary nssortdescriptor


    【解决方案1】:

    使用

    NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"dict.quantity1"  ascending:YES];
    stories=[stories sortedArrayUsingDescriptors:[NSArray arrayWithObjects:descriptor,nil]];
    

    关键是字典。数量1

    【讨论】:

    • @HariBabu,如果 Suen 这样做,您应该感谢 Suen 帮助解决了您的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-02
    • 1970-01-01
    • 1970-01-01
    • 2018-11-24
    • 2018-07-17
    相关资源
    最近更新 更多