我有一个包含 AEMEvent
自定义对象的 NSArray
。此 AEMEvent
类的属性之一是 NSDate
类型的 startDate
。
@interface AEMEvent : NSObject
{
//.....
NSDate *startDate;
//..........
}
我想根据此属性对此数组进行排序,因此输出将是一个 NSArray
,其中包含按 startDate
排序的 AEMEvent
对象属性。
我看到了这个有趣的答案,使用 -[NSArraysortedArrayUsingSelector:
或 -[NSMutableArray 对
并传递 NSDate
对象的 NSArray
进行排序sortUsingSelector:]@selector(compare:)
作为参数,并想知道是否有办法在我的情况下使用它 - Sort NSArray of date strings or objects
最佳答案
// Sort AMEvent by startDate
NSSortDescriptor * firstDescriptor = [[[NSSortDescriptor alloc]
initWithKey:@"startDate" ascending:YES
selector:@selector(caseInsensitiveCompare:)]
NSArray * descriptors = [NSArray arrayWithObjects:firstDescriptor, nil];
NSArray * sortedArray = [yourArray sortedArrayUsingDescriptors:descriptors];
关于cocoa - 基于 NSDate 类型的属性对自定义对象的 NSArray 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11584142/