Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting Array of CLLocationDegrees Objects

I'm trying to sort 2 arrays of CLLocationDegrees Objects such that I can determine a minimum and maximum latitude and longitude to centre a map with 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 sort the arrays I get the error: "() is not convertible to type [(CLLocationDegrees)]"

I'm not sure that I understand this, CLLocationDegree objects are stored as Double values, why can I not sort them in this manner?

like image 556
user3185748 Avatar asked Apr 06 '26 20:04

user3185748


2 Answers

Put this into a playground to see two ways of doing 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 performs the sort in place so you can't assign it to itself.

like image 138
Abizern Avatar answered Apr 08 '26 12:04

Abizern


Just try to read what compiler says. "() is not convertible to the type [(CLLocationDegrees)]. The () means Void. Thats right. Void is just typealiased empty tuple which is (). So the compiler says you are assigning Void to CLLocationDegrees array. That means latitudes.sort({ $0 < $1 }) returns Void.

From here you can continue to read @Abizern's answer.

like image 21
mustafa Avatar answered Apr 08 '26 13:04

mustafa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!