iphone – 使用NSSortDescriptor的非常自定义的订单

前端之家收集整理的这篇文章主要介绍了iphone – 使用NSSortDescriptor的非常自定义的订单前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
假设我有不同状态的对象.状态从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 ...)
  }
}

猜你在找的Xcode相关文章