【问题标题】:Sort Dictionary swift 3.0排序字典 swift 3.0
【发布时间】:2017-12-15 06:01:46
【问题描述】:

在编码的某些时候,我需要对字典进行排序以根据字典的键获取有序数据。

我尝试通过以下方式获得结果:

let sortedDict = dict.sorted { $0.key < $1.key }

结果是:

let dict = ["a0":1, "a3":2 , "a10":2, "a20":3]

虽然,想要的结果:

let dict = ["a0":1, "a10":2, "a20":3, "a3":2]

任何建议将不胜感激。

提前致谢。

【问题讨论】:

    标签: sorting dictionary swift3 unordered-set


    【解决方案1】:

    试试这个:

        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")
            }
        }
    

    简单的排序功能在这里不起作用。

    【讨论】:

      【解决方案2】:

      字典是一个无序的数据集合,所以你不能创建一个有序的字典。

      【讨论】:

      • 好的。感谢回复
      猜你喜欢
      • 2016-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-22
      • 2016-11-19
      • 2016-09-14
      • 2016-05-14
      • 1970-01-01
      相关资源
      最近更新 更多