在分组字典中对子数组进行排序

时间:2018-11-01 10:47:17

标签: swift sorting dictionary

我有3个数组,必须在同一年合并。我已经做到了,然后使用Dictionary.init(grouping: weatherDataArray)进行了分组,现在我正在尝试对分组字典进行排序。这是我拥有的数据的示例:

[2008: [["year": 2008, "month": 12, "rainfallValue": 87.9000015, "tminValue": 0.200000003, "tmaxValue": 6.0999999], ["year": 2008, "month": 11, "rainfallValue": 106.400002, "tminValue": 0.200000003, "tmaxValue": 6.0999999], ["year": 2008, "month": 10, "rainfallValue": 155.699997, "tminValue": 0.200000003, "tmaxValue": 6.0999999], ["year": 2008, "month": 9, "rainfallValue": 106.300003, "tminValue": 0.200000003, "tmaxValue": 6.0999999], ["year": 2008, "month": 8, "rainfallValue": 133.600006, "tminValue": 0.200000003, "tmaxValue": 6.0999999], ["year": 2008, "month": 7, "rainfallValue": 108.099998, "tminValue": 0.200000003, "tmaxValue": 6.0999999], ["year": 2008, "month": 6, "rainfallValue": 78.5, "tminValue": 0.200000003, "tmaxValue": 6.0999999], ["year": 2008, "month": 5, "rainfallValue": 47.9000015, "tminValue": 0.200000003, "tmaxValue": 6.0999999], ["year": 2008, "month": 4, "rainfallValue": 76.1999969, "tminValue": 0.200000003, "tmaxValue": 6.0999999], ["year": 2008, "month": 3, "rainfallValue": 122.5, "tminValue": 0.200000003, "tmaxValue": 6.0999999], ["year": 2008, "month": 2, "rainfallValue": 82.3000031, "tminValue": 0.200000003, "tmaxValue": 6.0999999], ["year": 2008, "month": 1, "rainfallValue": 189.600006, "tminValue": 0.200000003, "tmaxValue": 6.0999999]], 1992: [["year": 1992, "month": 6, "rainfallValue": 38.5999985, "tminValue": 3.20000005, "tmaxValue": 5.80000019], ["year": 1992, "month": 5, "rainfallValue": 57.2999992, "tminValue": 3.20000005, "tmaxValue": 5.80000019], ["year": 1992, "month": 4, "rainfallValue": 88.0, "tminValue": 3.20000005, "tmaxValue": 5.80000019], ["year": 1992, "month": 3, "rainfallValue": 125.0, "tminValue": 3.20000005, "tmaxValue": 5.80000019], ["year": 1992, "month": 2, "rainfallValue": 87.5, "tminValue": 3.20000005, "tmaxValue": 5.80000019], ["year": 1992, "month": 1, "rainfallValue": 77.4000015, "tminValue": 3.20000005, "tmaxValue": 5.80000019], ["year": 1992, "month": 12, "rainfallValue": 105.0, "tminValue": 3.20000005, "tmaxValue": 5.80000019], ["year": 1992, "month": 11, "rainfallValue": 158.5, "tminValue": 3.20000005, "tmaxValue": 5.80000019], ["year": 1992, "month": 10, "rainfallValue": 94.5, "tminValue": 3.20000005, "tmaxValue": 5.80000019], ["year": 1992, "month": 9, "rainfallValue": 115.5, "tminValue": 3.20000005, "tmaxValue": 5.80000019], ["year": 1992, "month": 8, "rainfallValue": 157.300003, "tminValue": 3.20000005, "tmaxValue": 5.80000019], ["year": 1992, "month": 7, "rainfallValue": 84.1999969, "tminValue": 3.20000005, "tmaxValue": 5.80000019]]]

基本上是[Int: [[String: Any]]]类型。

所以我将组作为年份,我的目标是制作一个数组,该数组按ascending的顺序按年份和月份进行排序,基本上是[[String: Any]],它将首先显示1992年的数据,而月份将从1-12年,然后是2008年,而今年的月份则与1-12年有关。

下一步应该做什么?还是有差异方法可以做到?

1 个答案:

答案 0 :(得分:3)

您可以首先根据关键字对分组字典进行排序,这样您将得到一个元组数组,该数组按年份进行排序,然后调用map并返回值,该值是字典,并根据月份在map调用中对它们进行排序。

我将您的问题中的值存储在groupedDict中。

let sortedArrFromDict = groupedDict.sorted(by: {$0.key < $1.key}).flatMap({$0.value.sorted(by: {$0["month"]! < $1["month"]!})})
print(sortedArrFromDict)