排序字典 swift 3.0
Sort Dictionary swift 3.0
在编码的某些时候,我需要根据字典的键对字典进行排序以获取有序数据。
我试图通过以下方式获得结果:
let sortedDict = dict.sorted { [=11=].key < .key }
结果为:
let dict = ["a0":1, "a3":2 , "a10":2, "a20":3]
虽然,期望的结果:
let dict = ["a0":1, "a10":2, "a20":3, "a3":2]
如有任何建议,我们将不胜感激。
提前致谢。
字典是无序的数据集合,因此您无法创建有序的字典。
试试这个:
for i in 0..<parameters!.count {
let key = "q\(i)"
if httpBody.contains(key){
let regex = try! NSRegularExpression(pattern: "\b\(key)\b", options: .caseInsensitive)
httpBody = regex.stringByReplacingMatches(in: httpBody, options: [], range: NSRange(0..<httpBody.utf16.count), withTemplate: "q")
}
}
简单的排序功能在这里不起作用。
在编码的某些时候,我需要根据字典的键对字典进行排序以获取有序数据。
我试图通过以下方式获得结果:
let sortedDict = dict.sorted { [=11=].key < .key }
结果为:
let dict = ["a0":1, "a3":2 , "a10":2, "a20":3]
虽然,期望的结果:
let dict = ["a0":1, "a10":2, "a20":3, "a3":2]
如有任何建议,我们将不胜感激。
提前致谢。
字典是无序的数据集合,因此您无法创建有序的字典。
试试这个:
for i in 0..<parameters!.count {
let key = "q\(i)"
if httpBody.contains(key){
let regex = try! NSRegularExpression(pattern: "\b\(key)\b", options: .caseInsensitive)
httpBody = regex.stringByReplacingMatches(in: httpBody, options: [], range: NSRange(0..<httpBody.utf16.count), withTemplate: "q")
}
}
简单的排序功能在这里不起作用。