I’m using NSArray *toWrite = [[items sortedArrayUsingFunction:comparator context:nil] copy]; within my code to sort the NSDictionaires in my NSArray.
Here is my C function for comparing:
static NSComparisonResult comparator( NSDictionary *d1, NSDictionary *d2, void *context )
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEE, dd/MM/yyyy"];
NSDate *date1 = [dateFormatter dateFromString:[d1 objectForKey:@"date"]];
NSDate *date2 = [dateFormatter dateFromString: [d2 objectForKey: @"dateString"]];
NSLog(@"date1:%@",[dateFormatter stringFromDate:date1]);
NSLog(@"date2:%@",[dateFormatter stringFromDate:date2]);
[dateFormatter release];
NSLog(@"COMPARE:%d",[date1 compare:date2]);
return [date1 compare:date2];
}
And Here is the output when I run the App with 3 NSDictionaries in the NSArray:
date1:Thu, 17/11/2011
date2:(null)
COMPARE:0
date1:Sat, 19/11/2011
date2:(null)
COMPARE:0
By the way here is how one NSDictionary from my .plist file:
<dict>
<key>date</key>
<string>Thu, 17/11/2011</string>
<key>duration</key>
<string>120 min</string>
<key>location</key>
<string>BA708</string>
<key>notes</key>
<string>10 min reading time</string>
<key>seat</key>
<string>14</string>
<key>time</key>
<string>09:00</string>
<key>unitName</key>
<string>ROBOT SYSTEMS DESIGN</string>
</dict>
You’re using “date” for one key and “dateString” for the other:
According to the plist example you show, the key is “date.” Assuming you’re comparing apples to apples, that’s why date2 always comes up null.