我试图搜索如何对 nsmutablearray 进行排序,但我只是更加困惑-如何对包含 10 个 NSNumber(可能为空)升序的 NSMutableArray 进行排序,如下所示-
NSMutableArray *a = [1,2,4,6,3,5,8,7,9,0]
对此- a = [0,1,2,3,4,5,6,7,8,9]
谢谢!
我试图搜索如何对 nsmutablearray 进行排序,但我只是更加困惑-如何对包含 10 个 NSNumber(可能为空)升序的 NSMutableArray 进行排序,如下所示-
NSMutableArray *a = [1,2,4,6,3,5,8,7,9,0]
对此- a = [0,1,2,3,4,5,6,7,8,9]
谢谢!
NSArray *a = @[@0,@1,@5,@2];
a = [a sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [(NSNumber *)obj1 compare:obj2] == NSOrderedDescending;
}];
NSMutableArray *a = [@[@1,@2,@4,@6,@3,@5,@8,@7,@9,@0,[NSNull null]] mutableCopy];
[a sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
if (obj2 == [NSNull null]) {
return YES;
}
return [(NSNumber *)obj1 compare:obj2] == NSOrderedDescending;
}];
NSArray *array = @[ @1, @2, @4, @6, @3, @5, @8, @7, @9, @0 ];
array = [array sortedArrayUsingSelector:@selector(compare:)];