Reputation: 531
I can't figure out how to sort my array. It's an array with dictionaries with dictionaries inside. So to get the startdate I would need to write
NSDictionary *object = [array objectAtIndex:0];
[[array objectForKey:@"DATE"]objectForKey:@"startDate"];
I want to sort the array, but because I need to sort it accordingly to the second dictionary I cant use this:
[array sortUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"startDate" ascending:YES]]];
I guess I just need to make some small adjustment, but I can't figure out how.
Upvotes: 1
Views: 472
Reputation: 35646
First of all the line that you get your objects should be called on object
instead of array
. Furthermore you could use valueForKeyPath
instead of objectForKey
for nested dictionaries. Something like this:
[object valueForKeyPath:@"DATE.startDate"];
Now you just apply the same thing to your sort descriptor:
[array sortUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"DATE.startDate" ascending:YES]]];
Keep in mind though that sortUsingDescriptors:ascending:
it's a method that sorts in place and must be called on an NSMutableArray
. If you just want a sorted copy of your array then use sortedArrayUsingDescriptors:
method instead. I hope that this makes sense...
Upvotes: 3