私の Xcode プロジェクトには、次のクラスがあります。
住所
@interface LDAddress : NSObject{
NSString *street;
NSString *zip;
NSString *city;
float latitude;
float longitude;
}
@property (nonatomic, retain) NSString *street;
@property (nonatomic, retain) NSString *zip;
@property (nonatomic, retain) NSString *city;
@property (readwrite, assign, nonatomic) float latitude;
@property (readwrite, assign, nonatomic) float longitude;
@end
位置
@interface LDLocation : NSObject{
int locationId;
NSString *name;
LDAddress *address;
}
@property (readwrite, assign, nonatomic) int locationId;
@property (nonatomic, retain) LDAddress *address;
@property (nonatomic, retain) NSString *name;
@end
UITableViewController のサブクラスには、LDLocations のソートされていないオブジェクトを多数含む NSArray があります。ここで、LDAddressのプロパティcityに基づいて昇順で NSArray のオブジェクトを並べ替えたいと思います。
NSSortDescriptor を使用して配列をソートするにはどうすればよいですか? 以下を試してみましたが、配列のソート時にアプリがダンプします。
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Address.city" ascending:YES];
[_locations sortedArrayUsingDescriptors:@[sortDescriptor]];