The key-value pairs in a Dictionary
are unordered. If you want to access them in a certain order, you must sort the keys yourself:
let dict = ["A": 1, "B": 2,"C": 3,"D": 4]
let str = dict.keys
.sorted(by: <)
.map { dict[$0]! }
.reduce ("") { $0 + String($1) }
Or alternatively:
let str = dict.keys
.sorted(by: <)
.map { String(dict[$0]!) }
.joined()
No idea about the relative performance of the two as I haven’t benchmarked them. But unless your dictionary is huge, the difference will be minimal.