我有一个类A对象的数组,让它成为detailsArray。使用NSSortDescriptor排序数组
A类有日期,名称为属性。
任何一个可以建议一个很好的方法来根据日期排序这个数组吗?如何使用NSSortDescriptor
对此数组进行排序?我知道使用NSSortDescriptor
分类字典的数组...
任何帮助是极大的赞赏.....
我有一个类A对象的数组,让它成为detailsArray。使用NSSortDescriptor排序数组
A类有日期,名称为属性。
任何一个可以建议一个很好的方法来根据日期排序这个数组吗?如何使用NSSortDescriptor
对此数组进行排序?我知道使用NSSortDescriptor
分类字典的数组...
任何帮助是极大的赞赏.....
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"date" ascending:NO];
NSArray *sortedArray = [detailsArray sortedArrayUsingDescriptors:@[sortDescriptor]];
应该是:sortedArrayUsingDescriptors – jomafer 2014-05-23 19:06:14
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];
}];
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"self" ascending:NO];
NSArray *descriptors = [NSArray arrayWithObject: descriptor];
NSArray *reverseOrder = [dateArray sortedArrayUsingDescriptors:descriptors];
您需要通过类名向关键字转发,如
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"ClassA.date" ascending:YES];
和做 Google的查询之前问任何问题,请参阅谷歌爬虫result.
这里http://stackoverflow.com/questions/13667288/how-to-sort-nsmutablearray-of检查我的答案-date-objects – 2013-03-21 06:32:03
我没有看到它,对不起。 – 2013-03-21 06:47:52