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

Arrays 如何按字符串键对字典排序?,arrays,swift,dictionary,swift4,Arrays,Swift,Dictionary,Swift4,我正在使用此代码对字典进行排序: var sortedArray = dict.sorted(by: {$0.0 < $1.0}) 插入 ["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,

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

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

 ["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(按:{$0.0<$1.0})
SorterDarray.removeLast()
sortedArray=dict.sorted(按:{Int($0.0)!
由于词典由字符串组成,因此这些字符串按字母顺序排序。您可以尝试以下内容:

let sortedArray = dict.sorted(by: {Int($0.0)! < Int($1.0)!})
let sortedArray=dict.sorted(按:{Int($0.0)!
编辑: 如果不是所有的钥匙都能转换成数字,我们必须使它更复杂:

let sortedArray = dict.sorted(by: {
  let a = Int($0.0) ?? 0
  let b = Int($1.0) ?? 0
  return a < b
})
let sortedArray=dict.sorted(按:{
设a=Int($0.0)??0
设b=Int($1.0)??0
返回a
这会将字典中的“名称”置于顶部。

因为进行比较的逻辑与给定元素相等的逻辑相关(您无法对不可比较元素的数组进行排序),所以输出将基于字符串比较的方式-因为给定的
dict
键是字符串-(“1”字符小于“2”)

但是,您可能需要将键值强制转换为int,然后进行排序:

let sorted = dict.sorted {
    if let key1Int = Int($0.key), let key2Int = Int($1.key) {
        return key1Int < key2Int
    }

    return true
}
let sorted=dict.sorted{
如果让key1Int=Int($0.key),则让key2Int=Int($1.key){
返回键1int<键2int
}
返回真值
}

此时,即使键值不可设置为Int,排序也应有效。

您可以使用通用实例方法对字典键进行排序

无论何时输入文件名或其他字符串,都应使用此方法 在列表和表格中显示,其中需要类似查找器的排序 适当。此方法的确切排序行为不同 在不同的区域设置下,并且可能在将来的版本中更改。此 方法使用当前区域设置


这是字典顺序,因为您处理的是字符串,而不是数字-
“2”比
“19”大"
@luk2302啊,好的。那么我需要做些什么来改变这一点呢?你能添加
dict
?它会更清晰…也许可以尝试远离字符串,而只使用int。@luk2302 dict是从firebase获取的,只允许我存储Strings致命错误:在打开可选文件时意外发现nilvalue@WillMa好的,所以这个解决方案还需要改进…@WillMays哦,这是因为其中一个键是“name”。这是故意的吗?请参阅更新question@WillMays我编辑了我的答案以考虑到这一点。到目前为止,我还能够测试我的代码。但请注意,您的一个值似乎无效(G.Snyder)。如果要保留它,则所有值必须是字符串(“0.34”、“G.Snyder”等)。
let sortedArray = dict.sorted(by: {
  let a = Int($0.0) ?? 0
  let b = Int($1.0) ?? 0
  return a < b
})
let sorted = dict.sorted {
    if let key1Int = Int($0.key), let key2Int = Int($1.key) {
        return key1Int < key2Int
    }

    return true
}
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")]