Sort a Dictionary in Swift

前端 未结 6 1071
情歌与酒
情歌与酒 2020-12-09 11:33

I know that this topic has been already discussed but I can\'t solve looking other answers, so sorry in advance for my ripetion!

I need to sort this Dictionary by ke

6条回答
  •  猫巷女王i
    2020-12-09 12:11

    Sorting your keys by the dictionary's value is actually simpler than it appears at first:

    let yourDict = ["One": "X", "Two": "B", "Three": "Z", "Four": "A"]
    let sortedKeys = yourDict.keys.sort({ (firstKey, secondKey) -> Bool in
        return yourDict[firstKey] < yourDict[secondKey]
    })
    

    And that's it! There's really nothing more to it.

提交回复
热议问题