【问题标题】:Reordering an array of NSDictionary [duplicate]重新排序NSDictionary数组[重复]
【发布时间】:2017-05-17 18:10:53
【问题描述】:

如何通过“ordinator”键以这种格式重新排序 NSDictionary 数组?

我需要通过“ordinator”键对它们重新排序,以便在表格中显示它们。在其他包含对象的数组中,我使用 sort 使用类属性进行排序。我可以做类似这个数组的事情吗?

 [
            [
            LogData = "17/05/2017 15:3:23";
            LogDataInclusao = "17/05/2017 15:3:23";
            LogNome = "Breno Bohm";
            LogNomeInclusao = "Breno Bohm";
            chave = I2017051715323LLNYB;
            chaveNcm = 129;
            date = "31/05/2017";
            defaultPrice = "R$829,44";
            discount1 = "0%";
            discount2 = "0%";
            discount3 = "0%";
            discount4 = "0%";
            discount5 = "0%";
            price = "R$829,44";
            productKey = 17392;
            quantity = 1;
        ],
            [
            LogData = "17/05/2017 15:3:23";
            LogDataInclusao = "17/05/2017 15:3:23";
            LogNome = "Breno Bohm";
            LogNomeInclusao = "Breno Bohm";
            chave = I2017051715323LFGCG;
            chaveNcm = 129;
            date = "31/05/2017";
            defaultPrice = "R$1.002,97";
            discount1 = "0%";
            discount2 = "0%";
            discount3 = "0%";
            discount4 = "0%";
            discount5 = "0%";
            price = "R$1.002,97";
            productKey = 17391;
            quantity = 1;
        ],
            [
            LogData = "17/05/2017 15:3:23";
            LogDataInclusao = "17/05/2017 15:3:23";
            LogNome = "Breno Bohm";
            LogNomeInclusao = "Breno Bohm";
            chave = I2017051715323KMPNY;
            chaveNcm = 129;
            date = "31/05/2017";
            defaultPrice = "R$732,75";
            discount1 = "0%";
            discount2 = "0%";
            discount3 = "0%";
            discount4 = "0%";
            discount5 = "0%";
            price = "R$732,75";
            productKey = 17394;
            quantity = 1;
        ],
            [
            LogData = "17/05/2017 15:3:23";
            LogDataInclusao = "17/05/2017 15:3:23";
            LogNome = "Breno Bohm";
            LogNomeInclusao = "Breno Bohm";
            chave = I2017051715323JHRYJ;
            chaveNcm = 747;
            date = "31/05/2017";
            defaultPrice = "R$270,44";
            discount1 = "0%";
            discount2 = "0%";
            discount3 = "0%";
            discount4 = "0%";
            discount5 = "0%";
            price = "R$270,44";
            productKey = 17393;
            quantity = 1;
        ]
    ]

【问题讨论】:

  • 协调键在哪里?

标签: ios sorting cocoa-touch nsarray


【解决方案1】:

是的,您可以对字典数组进行排序,只需使用subscript 而不是property 访问权限。

排序数组,键包含字符串值

let sortedArray = yourArray.sorted { $0[yourKey] as? String ?? "" < $1[yourKey] as? String ?? "" }

排序数组,键包含整数值

let sortedArray = yourArray.sorted { $0[yourKey] as? Int ?? 0 < $1[yourKey] as? Int ?? 0 }

注意:您的dictionary 中没有ordinator 键,因此只需将yourKey 替换为您想要对数组进行排序的key

【讨论】:

    【解决方案2】:

    您可以像这样在 Objective C 中实现这一点:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-27
      • 2012-08-11
      • 2017-11-17
      相关资源
      最近更新 更多