I'm trying to sort an array passing an argument to the selector. For instance, I have an array of locations and I want to sort this array based on the distance they have from a certain point (for instance, my current location).
This is my selector, however I don't know how to call it.
- (NSComparisonResult)compareByDistance:(POI*)otherPoint withLocation:(CLLocation*)userLocation {
int distance = [location distanceFromLocation:userLocation];
int otherDistance = [otherPoint.location distanceFromLocation:userLocation];
if(distance > otherDistance){
return NSOrderedAscending;
} else if(distance < otherDistance){
return NSOrderedDescending;
} else {
return NSOrderedSame;
}
}
I'm trying to sort the array using the following function, but I can't pass my location to the selector:
- (NSArray*)getPointsByDistance:(CLLocation*)location
{
return [points sortedArrayUsingSelector:@selector(compareByDistance:withLocation:)];
}