Sort Dictionary by Key Value

后端 未结 3 1063
萌比男神i
萌比男神i 2020-11-28 14:06
let dict: [String:Int] = [\"apple\":5, \"pear\":9, \"grape\":1]

How do you sort the dictionary based on the Int value so that the outp

3条回答
  •  心在旅途
    2020-11-28 14:32

    Dictionaries can't be sorted. Generally, when I need things sorted from a dictionary, I will make a separate array of my dictionary keys.

    In your case, make an array of the keys, sort them by comparing their values in the dictionary.

提交回复
热议问题