您应该能够使用您首先说的两个描述符来做到这一点,首先是按奖励,然后按标题。但是,您需要为awardOn 排序提供一个自定义的NSSortDescriptor,看起来像这样:
#define NULL_OBJECT(a) ((a) == nil || [(a) isEqual:[NSNull null]])
@interface AwardedOnSortDescriptor : NSSortDescriptor {}
@end
@implementation AwardedOnSortDescriptor
- (id)copyWithZone:(NSZone*)zone
{
return [[[self class] alloc] initWithKey:[self key] ascending:[self ascending] selector:[self selector]];
}
- (NSComparisonResult)compareObject:(id)object1 toObject:(id)object2
{
if (NULL_OBJECT([object1 valueForKeyPath:[self key]])) {
if (NULL_OBJECT([object2 valueForKeyPath:[self key]]))
return NSOrderedSame; // If both objects have no awardedOn field, they are in the same "set"
return NSOrderedDescending; // If the first one has no awardedOn, it is sorted after
}
if (NULL_OBJECT([object2 valueForKeyPath:[self key]])) {
return NSOrderedAscending; // If the second one has no awardedOn, it is sorted after
}
return NSOrderedSame; // If they both have an awardedOn field, they are in the same "set"
}
@end
这将允许您必须分开集合:在您的示例中,Awesome/Better/Cool 和 Another/Another One/One More/Yet another。在那之后,你应该擅长:
NSSortDescriptor *sortDescriptor1 = [[AwardedOnSortDescriptor alloc] initWithKey:@"awardedOn" ascending:YES];
NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES];
最后一点,您可能需要做更多的工作,具体取决于您的“空”奖励字段的外观(我假设,在上面的代码中,该字段设置为空)。你可以看看这里:https ://stackoverflow.com/a/3145789