-2

I'm looking for a way to sort an NSMutableArray of dictionaries based on dates contained in the dictionaries. Each one looks like this:

{
  name: tom, 
  surname: smith, 
  date of birth (NSDate): 2013-02-21 15:25:27
}

I know of the methods earlierDate etc., but since they only compare two dates I'm just a bit stuck on how to iterate through the array so that I come out with a fully ordered array of dictionaries?

jscs
  • 63,694
  • 13
  • 151
  • 195
Da49
  • 107
  • 2
  • 10

1 Answers1

5

using sortUsingComparator this can be solved easily:

[yourArray sortUsingComparator:^NSComparisonResult(NSDictionary *a, NSDictionary *b) {
   return [a[@"dateOfBirth"] compare:b[@"dateOfBirth"]];
}];
Jonathan Cichon
  • 4,396
  • 16
  • 19
  • Why was this downvoted? This is a viable approach for an `NSMutableArray` of dictionaries where the dictionaries have a key of `@"dateOfBirth"`. – rmaddy Feb 25 '13 at 19:51