0

オブジェクトのNSMutableArrayがあります。以下は構造体です。

//object at index i
locations {
   NSString* address;
   NSString* state;
   NSNumber* distance;
}

NSMutableArrayに上記の構造のような10000個のオブジェクトがあります場所がNSNumber距離順になるように、この配列をどのように並べ替えますか?

私はこれを試しました:

lowToHigh = [NSSortDescriptor sortDescriptorWithKey:@"distance" ascending:YES];
[locationArray sortUsingDescriptors:[NSArray arrayWithObject:lowToHigh]];

sortDescriptorWithKey:@"distance"間違って使用していますか?距離は実際にはキーではないため、NSNumber*距離です。

ヘッダー@property (strong, nonatomic)NSNumber* _distance;を編集してから@synthesize _distance = distance;メソッドファイルを編集しますが、これはlocationObjects。*オブジェクトクラスにあります。次に、これを現在のクラスにインポートして、この並べ替えを行っています。それは問題ですか?インポート方法はlocationObjects* locObj = [[locationObjects alloc] init];

4

1 に答える 1

1

これは私が使用するものです:

NSSortDescriptor *lowToHigh = [[NSSortDescriptor alloc] initWithKey:@"distance"
                                              ascending:YES];
NSArray *mySortDescriptors = [NSArray arrayWithObject:lowToHigh];
NSArray *sortedArray = [locationArray sortedArrayUsingDescriptors:mySortDescriptors];
于 2012-06-27T15:14:48.007 に答える