【发布时间】:2015-08-20 22:11:43
【问题描述】:
我正在尝试找出对包含 n 个字典的数组进行排序的最佳/最有效方法。每个字典中的键/值对之一是日期字段。将所有字典添加到数组后,我想按日期降序对数组进行排序。
例如,我有这样的代码:
NSMutableArray *myArray = [[NSMutableArray alloc] init];
NSMutableDictionary *dictionary1 = [[NSMutableDictionary alloc] init];
NSDate *today = [NSDate date];
[dictionary1 setObject:today forKey:@"date"];
[dictionary1 setObject:@"Another value" forKey:@"anotherKey"];
[myArray addObject:dictionary1];
NSMutableDictionary *dictionary2 = [[NSMutableDictionary alloc] init];
NSDate *tomorrow = [[NSDate date] dateByAddingTimeInterval:60*60*24];
[dictionary2 setObject:tomorrow forKey:@"date"];
[dictionary2 setObject:@"Yet another value" forKey:@"anotherKey"];
[myArray addObject:dictionary2];
现在我需要按日期降序对 myArray 进行排序。 (数组索引 0 应该是最新日期)
注意:在我的实际项目中,我不会以这种方式创建和添加字典。但是为了查看日期是如何存储在字典中的,假设我已将这两个放入数组中。
【问题讨论】:
标签: ios objective-c sorting nsmutablearray