排序

作者: 孙凯iOS | 来源:发表于2018-12-27 17:19 被阅读0次

    排序

    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

    相关文章

      网友评论

          本文标题:排序

          本文链接:https://www.haomeiwen.com/subject/rrldlqtx.html