I am trying to build a generic function that arranges the keys of a dictionary in ascending order. The code below doesn’t perform the task correctly. How do I resolve it?
func sortKeysx<T: Comparable>(value: [T:T])->[T:T] {
let sorted = value.sorted{ $0.key < $1.key }
return Dictionary(uniqueKeysWithValues: sorted)
}
sortKeysx(value: [5.0:3, 4.0:2, 3.0:7, 2.0:1])
output: [3.0: 7.0, 4.0: 2.0, 5.0: 3.0, 2.0: 1.0]