With a combination of the numbers.values.array and reduce functions, you can simplify this in a single line of code.
numbers.values.array.reduce([], combine: +)
However, I would like to note that since you are using a dictionary, you cannot guarantee that the values ββwill be sorted, so you can use the sorted function to do this:
sorted(numbers.values.array.reduce([], combine: +), <)
As @Jeffery Thomas explained, you can also use the flat map that was added in Swift 1.2:
sorted(numbers.values.array.flatMap { $0 }, <)
And to take another step using the global sorted function, < is extraneous, since it uses the reduce and flatMap global functions by flatMap , you can delete the array as indicated by Martin R, so it can be reduced to:
sorted(reduce(numbers.values, [], +)) sorted(flatMap(numbers.values) { $0 })
source share