2014-07-15 21 views
0

我有其中的每一个Stop对象的数组包含按距离排序的Objective-C

@property (nonatomic)CLLocationDistance distanceFromUser;

我怎么能枚举虽然阵列和排序他们最接近最远?这是我尝试过的代码,我没有得到一个排序数组。

NSSortDescriptor * ascendingDistance = [[NSSortDescriptor alloc] initWithKey:@"distanceFromUser" ascending:YES]; 
[_sortedStops sortedArrayUsingDescriptors:@[ascendingDistance]]; 

我将不得不使用一个“老派”的排序方法来做到这一点或没有对象 - 有一个功能,我可以用它来轻松地做到这一点?

回答

4

...我没有得到一个排序数组。

假设你的-distanceFromUser方法正常工作,你确实会得到一个有序的数组。但是你没有把这个数组分配给任何东西,所以你永远不会知道。

它看起来像你期望的阵列排序,但-sortedArrayUsingDescriptors:而不是返回一个新的数组。这样做:

NSSortDescriptor * ascendingDistance = [[NSSortDescriptor alloc] initWithKey:@"distanceFromUser" ascending:YES]; 
NSArray *sortedArray = [_sortedStops sortedArrayUsingDescriptors:@[ascendingDistance]]; 
+0

或使用'sortUsingDescriptors'作为可变数组 –