排序

2018-12-27  本文已影响0人  孙凯iOS

排序

NSArray *array = @[@2,@1,@3,@4,@6,@5];

降序

[array sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
    NSNumber *first = obj1;
    NSNumber *second = obj2;
    if ([first compare:second] == NSOrderedAscending) {
        return NSOrderedDescending;
    }
    return NSOrderedAscending;
}];

6, 5, 4, 3, 2, 1

升序

[array sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
    NSNumber *first = obj1;
    NSNumber *second = obj2;
    if ([first compare:second] == NSOrderedAscending) {
        return NSOrderedAscending;
    }
    return NSOrderedDescending;
}];

1, 2, 3, 4, 5, 6

反转

[array sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
    return NSOrderedDescending;
}];

5, 6, 4, 3, 1, 2
上一篇下一篇

猜你喜欢

热点阅读