How can I reorder an array of NSDictionary in this format by the "ordinator" key?
I need to reorder them by the "ordinator" key to show them in a table. In other arrays with objects I used sort to sort using a class property. Can I do something similar to this array?
[
[
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;
]
]
Yes, you can sort the the array of dictionary same way just need to use subscript
instead of property
access.
Sort array with key contains String value
let sortedArray = yourArray.sorted { $0[yourKey] as? String ?? "" < $1[yourKey] as? String ?? "" }
Sort array with key contains Integer value
let sortedArray = yourArray.sorted { $0[yourKey] as? Int ?? 0 < $1[yourKey] as? Int ?? 0 }
Note: There is no ordinator
key in your dictionary
so simply replace the yourKey
with key
with which you want to sort your array.