Sortieren Sie NSArray mit benutzerdefinierten Objekten
In meinem Xcode-Projekt habe ich folgende Klassen:
Adresse
@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
Ort
@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
In einer Unterklasse von UITableViewController gibt es ein NSArray, das viele unsortierte Objekte von LDLocations enthält. Jetzt möchte ich die Objekte des NSArray aufsteigend nach der Eigenschaft sortierenStadt einer LDAddress.
Wie kann ich das Array mit NSSortDescriptor sortieren? Ich habe Folgendes versucht, aber die App gibt beim Sortieren des Arrays einen Fehler aus.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Address.city" ascending:YES];
[_locations sortedArrayUsingDescriptors:@[sortDescriptor]];