views:

24

answers:

1

I have a bunch of airports with geocoordinates stored in a glist.

I am then using CLLocationManager to get 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?

+1  A: 

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:

  1. compute the distace for each airport coordinate from the curent distance in O(n), where n is number of coordinates
  2. 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? :)

joshis