У меня есть Codable данные, которые я пытаюсь отсортировать данные на основе значения, полученного из каждого индекса.
Данные:
[Film_Bee.TheatersView.Theaters(name: "American Cinematheque at the Aero
Theatre", theatreId: "2417", location: Film_Bee.TheatersView.Location(address:
Film_Bee.TheatersView.Address(street: "1328 Montana Ave.", state: "CA", city:
"Santa Monica", postalCode: "90403", country: "USA"), geoCode:
Film_Bee.TheatersView.Geo(latitude: "34.0319", longitude: "-118.4953"))),
Film_Bee.TheatersView.Theaters(name: "AMC Santa Monica 7", theatreId: "9153",
location: Film_Bee.TheatersView.Location(address:
Film_Bee.TheatersView.Address(street: "1310 3rd St.", state: "CA", city: "Santa
Monica", postalCode: "90401", country: "USA"), geoCode:
Film_Bee.TheatersView.Geo(latitude: "34.0167", longitude: "-118.4977"))),
Film_Bee.TheatersView.Theaters(name: "AMC Broadway 4", theatreId: "8241",
location: Film_Bee.TheatersView.Location(address:
Film_Bee.TheatersView.Address(street: "1441 Third Street Promenade", state:
"CA", city: "Santa Monica", postalCode: "90401", country: "USA"), geoCode:
Film_Bee.TheatersView.Geo(latitude: "34.0150", longitude: "-118.4948"))),
Film_Bee.TheatersView.Theaters(name: "Laemmle Monica Film Center", theatreId:
"7293", location: Film_Bee.TheatersView.Location(address:
Film_Bee.TheatersView.Address(street: "1332 2nd St.", state: "CA", city: "Santa
Monica", postalCode: "90401", country: "USA"), geoCode:
Film_Bee.TheatersView.Geo(latitude: "34.0157", longitude: "-118.4980")))]
Здесь я взял данные и нашелрасстояния от текущего местоположения:
let geo = theater.map { $0.location}.map { $0.geoCode }
let lat = geo.latitude
let long = geo.longitude
let theaterGeo = CLLocationCoordinate2D(latitude: Double(lat)!, longitude: Double(long)!)
let theaterLat = theaterGeo.latitude
let theaterLong = theaterGeo.longitude
let location = CLLocation(latitude: theaterLat, longitude: theaterLong)
let distance = self.location.distance(from: location)
Я сейчас пытаюсь взять расстояния и отсортировать данные в формате Codable по убыванию.Я знаю, как сортировать Codable данные в индексе с sorted(by: { $0.value > $1.value })
.Как я могу отсортировать индекс по значениям расстояния?
Обновление: если я попробую код ниже, я получаю ошибку Use of undeclared type Theaters
extension Array where Element == Theaters {
mutating func sort(by location: CLLocation) {
return sort(by: { $0.distance(to: location) < $1.distance(to: location) })
}
func sorted(by location: CLLocation) -> [Theaters] {
return sorted(by: { $0.distance(to: location) < $1.distance(to: location) })
}
}