I have a bunch of airports with geocoordinates stored in a glist.
I am then using CLLocationManager to g开发者_StackOverflowet current location.
I now want to go through each item in the glist and compare it to the current location and order the airports from closest to furthest.
My algorithm sucks - any ideas?
If you have CLLocation instances, you can get their relative distance using:
- (CLLocationDistance)distanceFromLocation:(const CLLocation *)location
Now you write:
My algorithm sucks - any ideas?
Well, what is your algorithm? Why does it suck? Any code you have?
From the high level perspective, I would do it like this:
- compute the distace for each airport coordinate from the curent distance in O(n), where n is number of coordinates
- sort the items using some stable algorithm like QuickSort (...NSArray's sort should work OK...) which takes you O(n*log n), or make it work so that you can use radix sort that work in O(n)
Is there a reason why this wouldn't work? :)
- CLLocation class reference
- Radix sort
精彩评论