Sort a Dictionary in Swift

前端 未结 6 1069
情歌与酒
情歌与酒 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条回答
  •  误落风尘
    2020-12-09 12:10

    You can't sort dictionary in such easy way. I think dictionary is using some sort of tree data structure. But after sorting you get an array of tuples. So you can get keys in such way:

        let codeValueDict = ["us": "$", "it": "€", "fr": "€"]
        let sortedKeysAndValues = sorted(codeValueDict) { $0.0 < $1.0 }
        let keys = sortedKeysAndValues.map {$0.0 }
        let values = sortedKeysAndValues.map {$0.1 }
    

提交回复
热议问题