Сортировать NSArray с пользовательскими объектами
В моем проекте XCode у меня есть следующие классы:
Address
@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
Location
@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 есть NSArray, содержащий много несортированных объектов LDLocations. Теперь я хочу отсортировать объекты NSArray по возрастанию на основе свойстваcity адрес LDA.
Как я могу отсортировать массив с помощью NSSortDescriptor? Я попробовал следующее, но приложение сбрасывает при сортировке массива.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Address.city" ascending:YES];
[_locations sortedArrayUsingDescriptors:@[sortDescriptor]];