重新排序 NSDictionary 数组

Reordering an array of NSDictionary

如何通过 "ordinator" 键重新排序这种格式的 NSDictionary 数组?

我需要按 "ordinator" 键重新排序以在 table 中显示它们。在其他带有对象的数组中,我使用 class 属性 进行排序。我可以做类似这个数组的事情吗?

 [
            [
            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 = "R9,44";
            discount1 = "0%";
            discount2 = "0%";
            discount3 = "0%";
            discount4 = "0%";
            discount5 = "0%";
            price = "R9,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.002,97";
            discount1 = "0%";
            discount2 = "0%";
            discount3 = "0%";
            discount4 = "0%";
            discount5 = "0%";
            price = "R.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 = "R2,75";
            discount1 = "0%";
            discount2 = "0%";
            discount3 = "0%";
            discount4 = "0%";
            discount5 = "0%";
            price = "R2,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 = "R0,44";
            discount1 = "0%";
            discount2 = "0%";
            discount3 = "0%";
            discount4 = "0%";
            discount5 = "0%";
            price = "R0,44";
            productKey = 17393;
            quantity = 1;
        ]
    ]

是的,你可以用同样的方式对字典数组进行排序,只需要使用 subscript 而不是 property 访问。

使用包含字符串值的键对数组进行排序

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

使用包含整数值的键对数组进行排序

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

注意: dictionary 中没有 ordinator 键,所以只需将 yourKey 替换为 key 即可你想对数组进行排序。

您可以在 Objective C 中实现此目的,如下所示:

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