我有 NSMutableDictionary(NSString 对象)的 NSMutableArray。NSString 对象之一实际上是一个日期,我需要根据该日期对 NSMutableArray 进行排序,并且我不希望它将日期排序为字符串。我怎样才能做到?
问问题
3876 次
2 回答
6
如果我理解正确,您的数组包含包含字符串的字典,并且您希望对这些字符串进行排序......作为日期。可能是这样的:
[someArray sortWithOptions: 0 usingComparator: ^(id inObj1, id inObj2) {
NSDate *date1 = [NSDate dateWithString: [inObj1 objectForKey: @"dateString"]];
NSDate *date2 = [NSDate dateWithString: [inObj2 objectForKey: @"dateString"]];
return [date1 compare: date2];
}];
于 2010-05-13T18:50:45.703 回答
1
您将需要使用sortedArrayUsingFunction:context:方法。例如:
NSInteger comparator( NSDictionary *d1, NSDictionary *d2, void *context )
{
return [[d1 objectForKey:@"date"] compare:[d2 objectForKey:@"date"]];
}
// In some method:
NSArray *sortedArray = [array sortedArrayUsingFunction:comparator context:nil];
注意:这未经测试。
于 2010-05-13T16:05:19.660 回答