您應該能夠通過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