Objective c 使用NSSortDescriptor对数组进行排序

Objective c 使用NSSortDescriptor对数组进行排序,objective-c,nsarray,nssortdescriptor,Objective C,Nsarray,Nssortdescriptor,我有一个A类对象数组,让它成为detailsArray 类具有日期、名称和属性 有谁能推荐一种基于日期对数组排序的好方法吗 如何使用NSSortDescriptor对此数组进行排序?我知道字典的排序数组是使用NSSortDescriptor 非常感谢您的帮助 NSSortDescriptor *sortDescriptor; sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"date"

我有一个A类对象数组,让它成为detailsArray

类具有日期、名称和属性

有谁能推荐一种基于日期对数组排序的好方法吗

如何使用
NSSortDescriptor
对此数组进行排序?我知道字典的排序数组是使用
NSSortDescriptor

非常感谢您的帮助

NSSortDescriptor *sortDescriptor;
sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"date"
                                              ascending:YES] autorelease];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSArray *sortedArray;
sortedArray = [yourArray sortedArrayUsingDescriptors:sortDescriptors];
或者也可以使用块

NSArray *sortedArray;
sortedArray = [yourArray sortedArrayUsingComparator:^NSComparisonResult(A *a, A *b) {
    NSDate *first = a.date;
    NSDate *second = b.date;
    return [first compare:second];
}];

您需要通过其类名为key forward,如

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"ClassA.date" ascending:YES];

在问任何问题之前,先用谷歌搜索你的查询,查看谷歌爬虫查看我的答案,我没有看到,对不起……应该是:使用描述符进行分类
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"date" ascending:NO];
NSArray *sortedArray = [detailsArray sortedArrayUsingDescriptors:@[sortDescriptor]];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"ClassA.date" ascending:YES];