Swift - 排序和分离字典数组

时间:2018-05-15 18:09:47

标签: arrays swift sorting dictionary

有没有办法根据参数值有效地对字典数组进行排序并为每个参数值返回单独的数组?

示例数组:

[["value":3, "groupID":1],
["value":5, "groupID":2],
["value":2, "groupID":1],
["value":6, "groupID":3],
["value":1, "groupID":2],
["value":9, "groupID":3]]

所需的返回输出1(已排序的数组):

[["value":2, "groupID":1],
["value":3, "groupID":1],
["value":1, "groupID":2],
["value":5, "groupID":2],
["value":6, "groupID":3],
["value":9, "groupID":3]]

所需的返回输出2(按参数分隔的数组):

[["value":2, "groupID":1],
["value":3, "groupID":1]]

[["value":1, "groupID":2],
["value":5, "groupID":2]]

[["value":6, "groupID":3],
["value":9, "groupID":3]]

我提出的一个解决方案非常缓慢,即:

//variable array is the master array of dictionaries

var sorted = [[Int:Int]]() 
//(Output 1) sorted is the sorted array
sorted = array.sorted { t1, t2 in
                if t1.groupID == t2.groupID {
                    return t1.value < t2.value
                }
                return t1.groupID < t2.groupID
            }

var separated = [Int:[Int:Int]]() 
//(Output 2) separated is a dictionary that contains separate arrays, all of which have the same of a designated property. Essentially the same thing as separate, distinct arrays sorted by parameter for Output 2
separated = [
            for i in 0..<sorted.count {
                separated[sorted[i].channel]?.append(sorted[i])
            }

有关如何加快速度的想法?谢谢!

2 个答案:

答案 0 :(得分:2)

这是一个简单的单行方法,可以帮助您入门:

["1": [["groupID": "1", "value": "3"], ["groupID": "1", "value": "2"]],
 "2": [["groupID": "2", "value": "5"], ["groupID": "2", "value": "1"]],  
 "3": [["groupID": "3", "value": "6"], ["groupID": "3", "value": "9"]]]

结果是一个字典由groupIDs的值键入:

[["value":"2", "groupID":"1"],
["value":"3", "groupID":"1"]]

[["value":"1", "groupID":"2"],
["value":"5", "groupID":"2"]]

[["value":"6", "groupID":"3"],
["value":"9", "groupID":"3"]]

好吧,考虑一下这个结果。字典的是您的三个“所需输出”数组:

values

您可以将这些值作为字典的keys来访问,也可以使用 body { background-color: #d4f9f7; font-family: 'Raleway', sans-serif; } #logoheader { background: url('../images/logosmall.jpg') no-repeat; width: 170px; height: 170px; margin-left: 30px; } #generalmenu { margin-top: 110px; font-size: 1.8em; } #navlist { font-size: 1.6em; background-color: #d4f9f7; } #mainpage { background: url('../images/prague_exper.jpg') no-repeat; background-size: cover; height: 50vw; box-shadow: 0 0 50px #ffff; border: 2px solid #ffff; } /* Get on Board + Learning & Development Consultancy */ #intestazione { text-align: center; } /* Get on Board */ #maintitle { color: #ffffff; font-size: 8vw; text-shadow: 1px 1px 1px #222; } /* Learning & Development Consultancy */ #subtitle { font-size: 2vw; text-shadow: 1px 1px 1px #222; color: #ffffff; } #footer { height: 200px; } /*Media Queries*/ @media (max-width: 768px) { #subtitle { visibility: hidden; } #logoheader { background: url('../images/logoxs.jpg') no-repeat; margin-left: 120px; } #mainheader { height: 120px; } #navlist { font-size: 1.1em; background-color: #d4f9f7; } #mainpage { margin-top: 60px; }按键排序,然后深入了解每个数组。

派生单个排序数组非常简单。

答案 1 :(得分:0)

这不是一个漂亮的实现,看起来很有效,但不确定是否比你的更快:

let array: [[String: String]] = [["value":"3", "groupID":"1"],
             ["value":"5", "groupID":"2"],
             ["value":"2", "groupID":"1"],
             ["value":"6", "groupID":"3"],
             ["value":"1", "groupID":"2"],
             ["value":"9", "groupID":"3"]]

// grouping
var dict: [String: [[String: String]]] = [:]
array.forEach { (element) in
    var elements: [[String: String]] = []
    let groupID = element["groupID"]!
    if dict.keys.contains(groupID) {
        elements = dict[groupID]!
    }
    elements.append(element)
    dict[groupID] = elements
}

// spliting and sorting
var final: [[String: [[String: String]]]] = []
dict.keys.sorted().forEach { (key) in
    let sorted = dict[key]!.sorted(by: { $0["value"]! < $1["value"]! })
    final.append([key: sorted])
}

print(final)