Sorting an array of CLLocationDegrees objects

I am trying to sort 2 arrays of CLLocationDegrees objects to determine the minimum and maximum latitude and longitude in order to center the map using Swift.

var latitudes = [CLLocationDegrees]()
var longditudes = [CLLocationDegrees]()

self.latitudes.append(mylocation.coordinate.latitude)
self.longditudes.append(mylocation.coordinate.longitude)

latitudes = latitudes.sort({ $0 < $1 })
longditudes = longditudes.sort({ $0 < $1 })

      

When I go to sorting arrays, I get an error: "() is not convertible to type [(CLLocationDegrees)]"

I'm not sure if I understand this, CLLocationDegree objects are stored as double values, why can't I sort them this way?

+3


source to share


2 answers


Put this on a pad to see two ways to do what you are trying to do

import UIKit
import CoreLocation

var latitudes : [CLLocationDegrees] = []
var longditudes :[CLLocationDegrees] = []

latitudes.append(100.0) // Just using a Double as an example
longditudes.append(120.0)

latitudes.sort() {
    $0 < $1
}

longditudes.sort({ $0 < $1 })

      



sort

does an in-place sort, so you can't assign it to yourself.

+3


source


Just try to read what the compiler says. "is ()

not convertible to type [(CLLocationDegrees)]

. ()

means Void

. Correct. Void

is just an empty string with the type that is ()

. So the compiler says you are assigning Void

to an array CLLocationDegrees

. That means it latitudes.sort({ $0 < $1 })

returns Void

."



You can continue reading @Abizern's answer here.

+2


source







All Articles