I have an array of dictionaries and I want to sort them alphabetically by the \"itemName\" key in each dictionary. How can I do this in Swift?
I want to sort the ite
I have an array of dictionaries and i am sorting my array with alphabetically by the "firstname" key in each dictionary. You can also sort your array by just putting your array name in this code and your keyName
/*
let descriptor: NSSortDescriptor = NSSortDescriptor(key: "firstname", ascending: true, selector: #selector(NSString.caseInsensitiveCompare(_:)))
let sortedResults: NSArray = yourArrayName.sortedArrayUsingDescriptors([descriptor])
self.user_Array = NSMutableArray(array: sortedResults)
*/
Hopefully it helps you.
You can resort to NSArray
's sortUsingDescriptors
functionality:
let sortedArray = (itemsArray as NSArray).sortedArrayUsingDescriptors([NSSortDescriptor(key: "itemName", ascending: true)]) as [[String:AnyObject]]
Swift > 3
let sortedArray = (itemsArray as NSArray).sortedArray(using: [NSSortDescriptor(key: "itemName", ascending: true)]) as! [[String:AnyObject]]
Not very Swift-ish, and you lose the type safety, but it does it's job.
And btw, you have a lot of forced casts in your code, which have the potential of crashing your app, you should consider switching to optional ones instead.
'$' /// items is array of dictionary /*
let items: [[String: Any]] = [
["itemName":"ZZ_Item", "itemDescription":"Homer"],
["itemName":"AA_Item", "itemDescription":"Marge"],
["itemName":"VV_Item", "itemDescription":"Bart"],
["itemName":"BB_Item", "itemDescription":"Lisa"],
["itemName":"XY_Item", "itemDescription":"Maggie"],
["itemName":"CC_Item", "itemDescription":"Ned"]
]
print(items)
*/ /// O/P un_sorted array
/// [["itemName": "ZZ_Item", "itemDescription": "Homer"], ["itemName": "AA_Item", "itemDescription": "Marge"], ["itemName": "VV_Item", "itemDescription": "Bart"], ["itemName": "BB_Item", "itemDescription": "Lisa"], ["itemName": "XY_Item", "itemDescription": "Maggie"], ["itemName": "CC_Item", "itemDescription": "Ned"]]
/// sort array by itemsName key
func itemsSort(p1:[String:Any], p2:[String:Any]) -> Bool {
guard let s1 = p1["itemName"] as? String, let s2 = p2["itemName"] as? String else {
return false
}
return s1 < s2
}
let sortedItemsArray = items.sorted { itemsSort(p1:$0, p2:$1) }
print(sortedItemsArray)
//// O/P sorted array
[["itemName": "AA_Item", "itemDescription": "Marge"], ["itemName": "BB_Item", "itemDescription": "Lisa"], ["itemName": "CC_Item", "itemDescription": "Ned"], ["itemName": "VV_Item", "itemDescription": "Bart"], ["itemName": "XY_Item", "itemDescription": "Maggie"], ["itemName": "ZZ_Item", "itemDescription": "Homer"]]
Probably something like this, using sort
:
let sortedArray = itemsArray.sort { $0["itemName"] < $1["itemName"] }
where "$0" and "$1" represent respectively, in the array, each dictionary and its successor.
If the compiler complains about the types because it doesn't know that the value for this key is a String, you can cast like this (as said by @Cristik in the comments):
let sortedArray = itemsArray.sort { ($0["itemName"] as? String) < ($1["itemName"] as? String) }
This worked for me:
let descriptor: NSSortDescriptor = NSSortDescriptor(key: "itemName", ascending: true)
let sortedResults: NSArray = favArray.sortedArray(using: [descriptor]) as NSArray