重新排序NSDictionary数组

时间:2017-05-17 18:10:53

标签: ios sorting cocoa-touch nsarray

如何通过“adjator”键对此格式的NSDictionary数组进行重新排序?

我需要通过“adjator”键对它们进行重新排序,以便在表格中显示它们。在其他具有对象的数组中,我使用了排序来使用类属性进行排序。我可以做一些类似这个数组的事吗?

 [
            [
            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;
        ]
    ]

2 个答案:

答案 0 :(得分: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 }

注意: ordinator中没有dictionary个密钥,因此只需将yourKey替换为您要对其进行排序的key阵列。

答案 1 :(得分:1)

您可以在Objective C中实现这一目标:

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