您应该能够通过awardedOn做到这一点使用两个描述像你先说,首先,再由称号。但是,您需要为awardedOn提供一个自定义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
这将让你有分离集:真棒/更好/酷和另一个/另1个/ 1更多/在另一个例子中。在这之后,你要善于用:
NSSortDescriptor *sortDescriptor1 = [[AwardedOnSortDescriptor alloc] initWithKey:@"awardedOn" ascending:YES];
NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES];
最后要注意,你可能需要一个豆蔻更多的工作,这取决于你的“空” awardedOn领域的样子(我以为,在上面的代码,该字段被设置为空)。你可以看看这里:https://stackoverflow.com/a/3145789
我写过一个基于Swift中的这个答案的类。 https://gist.github.com/evgeniyd/d974121b94b17931480c –
这是真棒尤金!谢谢:) 我还建议你添加另一个有空的对象在升序为真时。 – ameunier