Sort Array of Dictionaries by Key Value

后端 未结 5 1551
星月不相逢
星月不相逢 2020-11-30 15:18

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

相关标签:
5条回答
  • 2020-11-30 15:55

    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.

    0 讨论(0)
  • 2020-11-30 16:13

    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.

    0 讨论(0)
  • 2020-11-30 16:20

    '$' /// 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"]]

    0 讨论(0)
  • 2020-11-30 16:22

    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) }
    
    0 讨论(0)
  • 2020-11-30 16:22

    This worked for me:

    let descriptor: NSSortDescriptor = NSSortDescriptor(key: "itemName", ascending: true)
    let sortedResults: NSArray = favArray.sortedArray(using: [descriptor]) as NSArray
    
    0 讨论(0)
提交回复
热议问题