我在尝试对将参数传递给选择器的数组进行排序。 例如,我有一个位置数组,我想根据他们从某个点(例如,我的当前位置)的距离对这个数组进行排序。Objective-C:使用参数对数组进行排序
这是我的选择器,但我不知道如何调用它。
- (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;
}
}
我尝试使用下面的函数数组排序,但我不能把我的位置选择:
- (NSArray*)getPointsByDistance:(CLLocation*)location
{
return [points sortedArrayUsingSelector:@selector(compareByDistance:withLocation:)];
}
在这里IMO是一个非常好的解决方案。 – Chuck 2011-03-30 03:21:53
这个解决方案正是我想要做的。弗拉基米尔的解决方案可以完成工作,但是这个更漂亮了;) – ffleandro 2011-04-04 12:19:16
+1。块是真棒:) – Vladimir 2011-04-04 12:21:52