Given the following NSSortDescriptor
for strings with Core Data:
[NSSortDescriptor sortDescriptorWithKey:@"series" ascending:true selector:@selector(caseInsensitiveCompare:)]
The results are correctly ordered alphabetically ascending. However in instances where series
is nil
, strings with nil
values are placed at the top, with non-nil values being sorted thereafter, E.G:
[nil, nil, nil, A, B, C, D...]
Is there any way to control this behavior? Core Data does not allow for custom selectors. Here's a similar question to mine (not addressing Core Data's limitation, however):
NSSortDescriptor and nil values
While you cannot use a custom selector with Core Data, you can subclass NSSortDescriptor
to change the default behavior. Something like this should work:
#define NULL_OBJECT(a) ((a) == nil || [(a) isEqual:[NSNull null]])
@interface NilsLastSortDescriptor : NSSortDescriptor {}
@end
@implementation NilsLastSortDescriptor
- (id)copyWithZone:(NSZone*)zone
{
return [[[self class] alloc] initWithKey:[self key]
ascending:[self ascending] selector:[self selector]];
}
- (id)reversedSortDescriptor
{
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]]) &&
NULL_OBJECT([object2 valueForKeyPath:[self key]]))
return NSOrderedSame;
if (NULL_OBJECT([object1 valueForKeyPath:[self key]]))
return NSOrderedDescending;
if (NULL_OBJECT([object2 valueForKeyPath:[self key]]))
return NSOrderedAscending;
return [super compareObject:object1 toObject:object2];
}
@end
Swift 4.1 version:
class NilsLastSortDescriptor: NSSortDescriptor {
override func copy(with zone: NSZone? = nil) -> Any {
return NilsLastSortDescriptor(key: self.key, ascending: self.ascending, selector: self.selector)
}
override var reversedSortDescriptor: Any {
return NilsLastSortDescriptor(key: self.key, ascending: !self.ascending, selector: self.selector)
}
override func compare(_ object1: Any, to object2: Any) -> ComparisonResult {
if (object1 as AnyObject).value(forKey: self.key!) == nil && (object2 as AnyObject).value(forKey: self.key!) == nil {
return .orderedSame
}
if (object1 as AnyObject).value(forKey: self.key!) == nil {
return .orderedDescending
}
if (object2 as AnyObject).value(forKey: self.key!) == nil {
return .orderedAscending
}
return super.compare(object1, to: object2)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With