按NSDate排序字典数组

Bau*_*aub 4 iphone xcode objective-c ios

我有一系列字典.在每个字典中,有dateOfInfo一个键(一个NSDate)和其他几个东西.我想按每个字典对数组进行排序,dateOfInfo最新的是第一个结果.

我怎样才能做到这一点?

Pen*_*One 16

您可以使用NSSortDescription例如排序

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey: @"dateOfInfo" ascending: NO];
NSArray *sortedArray = [array sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
[sortDescriptor release];
Run Code Online (Sandbox Code Playgroud)

您也可以使用该方法

- (NSArray *)sortedArrayUsingFunction:(NSInteger (*)(id, id, void *))comparator context:(void *)context
Run Code Online (Sandbox Code Playgroud)