假设我有不同状态的对象.状态从0到2.我需要使用NSSortDescriptor以这种方式对它们进行排序:
1
2
0
有什么建议?
解决方法
像这样(未经测试):
descriptor = [[[NSSortDescriptor alloc] initWithKey:@"status" ascending:YES selector:@selector(customStatusCompare:)] autorelease]; @interface NSNumber (CustomStatusCompare) - (NSComparisonResult)customStatusCompare:(NSNumber*)other; @end @implementation NSNumber (CustomStatusCompare) - (NSComparisonResult)customStatusCompare:(NSNumber*)other { NSAssert([other isKindOfClass:[NSNumber class]],@"Must be a number"); if ([self isEqual:other]) { return NSOrderedSame; } else if (... all your custom comparison logic here ...) } }