I am making an application where in a table View i want to show to the user places from the closest to the farest. I am using this function to calculate the distance between the user location and these places :
var distanceToUsersCurrentLocation: Double {
let manager = CLLocationManager()
let destinationCoordinates = CLLocation(latitude: (self.getLat() as NSString).doubleValue, longitude: (self.getLon() as NSString).doubleValue)
let selfCoordinates = CLLocation(latitude: (manager.location?.coordinate.latitude)!, longitude: (manager.location?.coordinate.longitude)!)
return selfCoordinates.distance(from: destinationCoordinates)
And now to sort my array I am using this function :
self.ListeChantiers?.sort(by: {ji,jij in ji.distanceToUsersCurrentLocation.isLess(than:jij.distanceToUsersCurrentLocation)})
This is working but the problem is that it's taking way too much time. Can someone tell me if it's mandatory or if there is a quicker solution ? Thanks for reading (and maybe helping) !