NSSortDescriptor *sortDescriptor;
sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"date"
ascending:YES] autorelease];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
self.sortedArray = [self.originalArray sortedArrayUsingDescriptors:sortDescriptors];
如果你的日期鍵實際上包含一個Date對象,如果它包含字符串..形式的日期比你將不得不嘗試這個這應該工作..
[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];
}];
我想什麼你想是好的在這裏描述:[如何排序一個NSMutableArray與自定義對象?](http://stackoverflow.com/questions/805547/how-to-sort-an-nsmutablearray-with-custom-objects-in - 您可以根據您的要求修改答案。 – Bhavin