Question

I have an array of CLLocation objects and I'd like to be able to compare them to get distance from a starting CLLocation object. The math is straight forward but I'm curious if there is a convenience sort descriptor to go about doing this? Should I avoid NSSortDescriptor and write a custom compare method + bubble sort? I'm usually comparing at most 20 objects, so it doesn't need to be super efficient.

Was it helpful?

Solution

You can write a simple compareToLocation: category for CLLocation that returns either NSOrderedAscending, NSOrderedDescending, or NSOrderedSame depending on the distances between self and the other CLLocation object. Then simply do something like this:

NSArray * mySortedDistances = [myDistancesArray sortedArrayUsingSelector:@selector(compareToLocation:)];

Edit:

Like this:

//CLLocation+DistanceComparison.h
static CLLocation * referenceLocation;
@interface CLLocation (DistanceComparison)
- (NSComparisonResult) compareToLocation:(CLLocation *)other;
@end

//CLLocation+DistanceComparison.m
@implementation CLLocation (DistanceComparison)
- (NSComparisonResult) compareToLocation:(CLLocation *)other {
  CLLocationDistance thisDistance = [self distanceFromLocation:referenceLocation];
  CLLocationDistance thatDistance = [other distanceFromLocation:referenceLocation];
  if (thisDistance < thatDistance) { return NSOrderedAscending; }
  if (thisDistance > thatDistance) { return NSOrderedDescending; }
  return NSOrderedSame;
}
@end


//somewhere else in your code
#import CLLocation+DistanceComparison.h
- (void) someMethod {
  //this is your array of CLLocations
  NSArray * distances = ...;
  referenceLocation = myStartingCLLocation;
  NSArray * mySortedDistances = [distances sortedArrayUsingSelector:@selector(compareToLocation:)];
  referenceLocation = nil;
}

OTHER TIPS

To improve on Dave's answer...

As of iOS 4, you can use a comparator block and avoid having to use a static variable and category:

NSArray *sortedLocations = [self.locations sortedArrayUsingComparator:^NSComparisonResult(CLLocation *obj1, CLLocation *obj2) {
    CLLocationDistance distance1 = [targetLocation distanceFromLocation:loc1];
    CLLocationDistance distance2 = [targetLocation distanceFromLocation:loc2];

    if (distance1 < distance2)
    {
        return NSOrderedAscending;
    }
    else if (distance1 > distance2)
    {
        return NSOrderedDescending;
    }
    else
    {
        return NSOrderedSame;
    }
}];

Just to add to the category response (which is the way to go), don't forget you don't actually need to do any math yourself, you can use the CLLocation instance method:

- (CLLocationDistance)getDistanceFrom:(const CLLocation *)location

To get the distance between two location objects.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top