1

I have Codable data that I am attempting to sort the data based on a value obtained from each index.

Data:

[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")))]

Here I have taken the data and found the distances from current location:

        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)

I now am attempting to take the distances and sort the Codable data from lowest to highest. I know how to sort Codable data within the index with sorted(by: { $0.value > $1.value }). How can I sort the index by the distance values?

Update: If I try the code below I get the error 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) })
}
}
JSharpp
  • 459
  • 1
  • 9
  • 21
  • https://stackoverflow.com/questions/35199363/sort-array-by-calculated-distance-in-swift ? – Larme Dec 17 '18 at 15:30
  • I received an error that it did not conform to decodable or encodable protocol. – JSharpp Dec 17 '18 at 15:52
  • I think I need to take the array and mutate it with the distance for each item similar to your posted question but I'm not sure how when using codable data. – JSharpp Dec 17 '18 at 16:20
  • 1
    Regarding *Declaration is only valid at file scope*: The extension must be declared outside of any class or struct – vadian Dec 17 '18 at 16:37
  • Now I'm getting the error `Use of undeclared type Theaters` – JSharpp Dec 17 '18 at 17:21
  • Declare `Theaters` also on the root level or use a key path like `ClassEnclosingTheaters.Theaters` – vadian Dec 17 '18 at 17:25

0 Answers0