我有可编码数据,我试图根据从每个索引获得的值对数据进行排序。
数据:
[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)
我现在正在尝试将距离和可编码数据从最低到最高排序。我知道如何使用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) })
}
}