我们如何像示例中那样在Swift中创建排序的Dictionary?

数组中的值数据是随机的。值数组可能更长。


var exampleData = [ [["Section1" : ["11","Green"]   ],
                     [["Section1" : ["23","Blue"]    ],
                     [["Section1" : ["344","Yellow"]  ],
                     [["Section2" : ["100","Tall"]    ],
                     [["Section2" : ["232","Short"]   ],
                     [["Section3" : ["112","Number"]  ],
                     [["Section3" : ["222","String"]  ],


var sortedData = [["Section1" : [ ["11","Green"],  ["23","Blue"],["344","Yellow"] ]],
                  ["Section2" : [ ["100","Tall"],  ["232","Short"] ]],
                  ["Section3" : [ ["112","Number"],["222","String"] ]]]

最佳答案

你不知道但是您可以将所有值放入一个元组数组并对它们进行排序。或者,仅从元组数组开始,并且仅在以下情况下添加值:


如果键值是唯一的(每个数组索引中元组的第一个值),或者
如果键值已存在,则使用要输入的新元组更新值的元组。


每次添加新项目后,一定要求助。

10-02 02:14