0

我试图搜索如何对 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]

谢谢!

4

2 回答 2

2
NSArray *a = @[@0,@1,@5,@2];
a = [a sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
    return [(NSNumber *)obj1 compare:obj2] == NSOrderedDescending;
}];

带有 [NSNull null] 的可变和单词

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;
}];
于 2013-09-15T12:54:46.650 回答
2
 NSArray *array = @[ @1, @2, @4, @6, @3, @5, @8, @7, @9, @0 ];

array = [array sortedArrayUsingSelector:@selector(compare:)];
于 2013-09-15T12:57:13.193 回答