Assuming you have a model Placefor your objects:
class Place {
var latitude: CLLocationDegrees
var longitude: CLLocationDegrees
var location: CLLocation {
return CLLocation(latitude: self.latitude, longitude: self.longitude)
}
func distance(to location: CLLocation) -> CLLocationDistance {
return location.distance(from: self.location)
}
}
Then the array var places: [Place]can be sorted as such:
places.sort(by: { $0.distance(to: myLocation) < $1.distance(to: myLocation) })
source
share