如何按字符串键排序字典?

时间:2017-09-30 22:50:42

标签: arrays swift dictionary swift4

我正在使用此代码对字典进行排序:

var sortedArray = dict.sorted(by: {$0.0 < $1.0})

它正在排序,但不是我希望它在下面看到的方式:

["1", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "2", "20", "21", "22", "23", "24", "3", "4", "5", "6", "7", "8", "9"]

的内容
 ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"]

完整的词典:

["17": 00:00:89, "12": 00:00:89, "20": 00:00:89, "23": 00:00:89, "19": 00:00:89, "22": 00:00:89, "13": 00:00:89, "9": 00:00:00, "8": 00:00:00, "6": 00:00:89, "7": 00:17:13, "name": G. Snyder, "24": 00:00:89, "14": 00:00:89, "16": 00:00:89, "18": 00:00:89, "15": 00:00:89, "2": 00:02:02, "11": 00:00:89, "1": 00:01:01, "3": 00:01:59, "4": 00:03:12, "21": 00:00:89, "10": 00:00:33, "5": 00:06:15]
编辑:(仍然没有工作)

var sortedArray = dict.sorted(by: {$0.0 < $1.0})
sortedArray.removeLast()
sortedArray = dict.sorted(by: {Int($0.0)! < Int($1.0)!})

3 个答案:

答案 0 :(得分:2)

您可以使用通用实例方法localizedStandardCompare

对字典键进行排序
  

只要文件名或其他字符串是,就应该使用此方法   在类似Finder的排序的列表和表格中显示   适当。这种方法的确切排序行为是不同的   在不同的语言环境下,可能会在将来的版本中更改。这个   method使用当前的语言环境。

let dict = ["17": "00:00:89", "12": "00:00:89", "20": "00:00:89", "23": "00:00:89", "19": "00:00:89", "22": "00:00:89", "13": "00:00:89", "9": "00:00:00", "8": "00:00:00", "6": "00:00:89", "7": "00:17:13", "name": "G. Snyder", "24": "00:00:89", "14": "00:00:89", "16": "00:00:89", "18": "00:00:89", "15": "00:00:89", "2": "00:02:02", "11": "00:00:89", "1": "00:01:01", "3": "00:01:59", "4": "00:03:12", "21": "00:00:89", "10": "00:00:33", "5": "00:06:15"]

let sortedTuples = dict.sorted{$0.key.localizedStandardCompare($1.key) == .orderedAscending}
sortedTuples  // [(key "1", value "00:01:01"), (key "2", value "00:02:02"), (key "3", value "00:01:59"), (key "4", value "00:03:12"), (key "5", value "00:06:15"), (key "6", value "00:00:89"), (key "7", value "00:17:13"), (key "8", value "00:00:00"), (key "9", value "00:00:00"), (key "10", value "00:00:33"), (key "11", value "00:00:89"), (key "12", value "00:00:89"), (key "13", value "00:00:89"), (key "14", value "00:00:89"), (key "15", value "00:00:89"), (key "16", value "00:00:89"), (key "17", value "00:00:89"), (key "18", value "00:00:89"), (key "19", value "00:00:89"), (key "20", value "00:00:89"), (key "21", value "00:00:89"), (key "22", value "00:00:89"), (key "23", value "00:00:89"), (key "24", value "00:00:89"), (key "name", value "G. Snyder")]

答案 1 :(得分:1)

因为sorted(by:)如何进行比较的逻辑与给定元素的相等逻辑有关(你不能对不可比元素的数组进行排序),输出将基于如何字符串比较应该是 - 因为给定的let sorted = dict.sorted { if let key1Int = Int($0.key), let key2Int = Int($1.key) { return key1Int < key2Int } return true } 键是字符串 - (&#34; 1&#34;字符小于&#34; 2&#34;)。

但是,您可能需要将键值转换为Ints然后排序:

{{1}}

此时,即使键值不可投射到Int。

,排序也应该有效

答案 2 :(得分:-1)

由于您的词典由字符串组成,因此按字母顺序排序。您可以尝试的是这样的事情:

let sortedArray = dict.sorted(by: {Int($0.0)! < Int($1.0)!})

编辑: 如果不是所有的键都可以转换为数字,我们必须使它更复杂:

let sortedArray = dict.sorted(by: {
  let a = Int($0.0) ?? 0
  let b = Int($1.0) ?? 0
  return a < b
})

这会将“词典”中的“名称”置于顶部。