NSSortDescriptor *sortDescriptor;
sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"date"
ascending:YES] autorelease];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
self.sortedArray = [self.originalArray sortedArrayUsingDescriptors:sortDescriptors];
如果您的日期键实际上包含一个日期对象并且如果它包含一个字符串形式的日期,这应该可以工作..您将不得不尝试这个..
[resultArray sortUsingComparator:^NSComparisonResult(id a, id b) {
static NSDateFormatter *dateFormatter = nil;
if (!dateFormatter) {
dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"mm/dd/yyyy hh:mm:ss";
}
NSString *date1String = [a valueForKey:@"StartDate"];
NSString *date2String = [b valueForKey:@"StartDate"];
NSDate *date1 = [dateFormatter dateFromString:date1String];
NSDate *date2 = [dateFormatter dateFromString:date2String];
return [date1 compare:date2];
}];