Im sure i cant be the only one to have an app needing this functionality, yet i cant come across and Library to help with it thus far.
I have an Dictionary such as
[["order": 31399, "builder": "Test", "area": "North"], ["order": 4244, "builder": "Test", "area": "North"]["order": 41499, "builder": "new", "area": "South"]]Im wanting to group this data by Area in this case into a dictionary like so
[["North": [["order": 31399, "builder": "Test", "area": "North"], ["order": 4244, "builder": "Test", "area": "North"]]],["South": [["order": 41499, "builder": "new", "area": "South"]]]]Or maybe even 2 levels down, by area and then by builder in the area. In JS i have used D3 and this process is super simple with nesting, in swift its becoming a nightmare. Even if i can just get 1 level grouping i will be happy at this point. Any help is appreciated. Thanks
No methods for grouping cannot be found in the Standard Library. You might need to do it yourself.
let dicts: [[String: AnyObject]] = [["order": 31399, "builder": "Test", "area": "North"], ["order": 4244, "builder": "Test", "area": "North"],["order": 41499, "builder": "new", "area": "South"]]
func groupByArea(acc: [String: [[String: AnyObject]]], elt: [String: AnyObject]) -> [String: [[String: AnyObject]]] {
var result = acc
let key = (elt["area"] as? String) ?? "*unspecified*"
if result[key] == nil {
result[key] = []
}
result[key]!.append(elt)
return result
}
let grouped = dicts.reduce([:], combine: groupByArea)