I have a Firebase database with place names and coordinates, having fetched both and putting each of them into their own Array and then into a Dictionary, I want the Dictionary sorted by the nearest location/lowest Double.
I have looked at tonnes of previously asked questions and just can't seem to figure out what I'm doing wrong. When I print my dictionary it orders them in alphabetical order (having crosschecked the (actual) distances I know they are correct).
How would I order by lowest Double?
var placeName = ""
var distanceArray = [Double]()
var nameDistanceDictionary = [String:Double]()
Inside fetching data from Firebase
self.placeName = placeSnap.key
let distanceRound = Double(round(10*distance)/10)
self.distanceArray.append(distanceRound)
self.nameDistanceDictionary = [self.placeName:distanceRound]
print(self.nameDistanceDictionary)
//Prints (example Doubles)
["Ascot": 5.5]
["Birmingham": 1.2]
["Bristol" : 18.6]
["London": 0.3]
["Manchester": 40.2]
["Newcastle": 2.4]
I've tried (This prints the same as above minus the [""]
for (k,v) in (Array(self.nameDistanceDictionary).sorted {$0.1 < $1.1}) {
print("\(k):\(v)")
}