通过NSDate在NSMutableArray中对NSDictionaries进行sorting

我有一个NSMutableArray与字典,所有的字典包含一个NSDate是NSString的forms与键'date'。 我想按date排列NSDictionaries。 所以例如我有以下状态的数组:

Dict Date 20.06.1996 23:30 Dict Date 04.10.2011 19:00 Dict Date 20.06.1956 23:39 

我想对它进行sorting,看起来像这样:

 Dict Date 20.06.1956 23:39 Dict Date 20.06.1996 23:30 Dict Date 04.10.2011 19:00 

我已经尝试了NSSortDescriptor,但没有成功…

更新:

我设法sorting的date,但我来到这个问题:在字典中不仅有date,还有其他对象,我的代码做的只是切换date值之间的字典,而不是切换完整的字典周围。 有了这个,在字典中的其他值被分配了一个错误的date,这是非常糟糕的。 有谁能够帮助我? 下面是我的代码:

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *path = [documentsDirectory stringByAppendingPathComponent:@"savedData.daf"]; NSMutableArray *d = [NSMutableArray arrayWithContentsOfFile:path]; for (int ii = 0; ii < [d count]; ii++) { NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init]; if (is24h) { [dateFormatter setDateFormat:@"dd.MM.yyyy HH:mm"]; } else { [dateFormatter setDateFormat:@"dd.MM.yyyy hh:mm a"]; } [dateFormatter setLocale:[NSLocale currentLocale]]; NSDate *dat = [dateFormatter dateFromString:[[d valueForKey:@"Date"] objectAtIndex:ii]]; NSMutableDictionary *newDict = [[NSMutableDictionary alloc] init]; NSDictionary *oldDict = (NSDictionary *)[d objectAtIndex:ii]; [newDict addEntriesFromDictionary:oldDict]; [newDict setObject:dat forKey:@"Date"]; [d replaceObjectAtIndex:ii withObject:newDict]; [newDict release]; } NSSortDescriptor *sorter = [[NSSortDescriptor alloc] initWithKey:@"Date" ascending:YES]; NSArray *sorters = [[NSArray alloc] initWithObjects:sorter, nil]; [sorter release]; NSMutableArray *sorted = [NSMutableArray arrayWithArray:[d sortedArrayUsingDescriptors:sorters]]; [sorters release]; NSLog(@"%@",sorted); for (int ii = 0; ii < [sorted count]; ii++) { NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init]; if (is24h) { [dateFormatter setDateFormat:@"dd.MM.yyyy HH:mm"]; } else { [dateFormatter setDateFormat:@"dd.MM.yyyy hh:mm a"]; } [dateFormatter setLocale:[NSLocale currentLocale]]; NSString *sr = [dateFormatter stringFromDate:[[sorted valueForKey:@"Date"] objectAtIndex:ii]]; NSMutableDictionary *newDict = [[NSMutableDictionary alloc] init]; NSDictionary *oldDict = (NSDictionary *)[d objectAtIndex:ii]; [newDict addEntriesFromDictionary:oldDict]; [newDict setObject:sr forKey:@"Date"]; [sorted replaceObjectAtIndex:ii withObject:newDict]; [newDict release]; } NSLog(@"before: %@" "" "after: %@",d,sorted); [sorted writeToFile:path atomically:YES]; 

有很多方法可以做到这一点,一个是使用NSDate对象,而不是NSStrings(或根据ISO 8601格式化的NSString,以便字典sorting匹配所需的sorting)。 那么你可以这样做:

 NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"Date" ascending:YES]; [array sortUsingDescriptors:[NSArray arrayWithObject:descriptor]]; 

或者,如果您不能(或不想)更改数据,则可以使用块进行sorting:

 [array sortUsingComparator:^(id dict1, id dict2) { NSDate *date1 = // create NSDate from dict1's Date; NSDate *date2 = // create NSDate from dict2's Date; return [date1 compare:date2]; }]; 

当然,这可能会比第一种方法慢,因为通常最终会创build多个NSDate对象。

有几个选项,一个是把NSDate对象放在字典中。

比较string的一个问题是,你不能只做string比较,因为年份不在string的最重要的部分。

所以,你需要编写一个比较方法来使用:

 - (NSArray *)sortedArrayUsingComparator:(NSComparator)cmptr 

也许:

 - (NSArray *)sortedArrayUsingFunction:(NSInteger (*)(id, id, void *))comparator context:(void *)context 

比较器将需要处理dd.mm.yyyy hh:mmstring格式。

其他选项包括添加另一个字典键与NSDate表示和sorting。