美文网首页
iOS - sortedArrayUsingComparator

iOS - sortedArrayUsingComparator

作者: Simple_Code | 来源:发表于2020-04-21 16:16 被阅读0次

    sortedArrayUsingComparatorcompare配合使用可以写出更优雅的代码
    代码:

    // 定义一个数组数组
        NSArray *array = @[@(3),@(5),@(4),@(2),@(1),@1,@4];
        // 对数组进行排序(升序)
        NSArray *resultAscending = [array sortedArrayUsingComparator:^NSComparisonResult(NSNumber *obj1, NSNumber *obj2) {
            NSLog(@"%@ ~ %@", obj1, obj2);
            // 方法一:(推荐)
            return [obj1 compare:obj2];
            
            // 方法二:
            NSComparisonResult result =  [obj1 compare:obj2];
            if (result == NSOrderedAscending) {
                return NSOrderedAscending;// 3 5
            } else if (result == NSOrderedDescending) {
                return NSOrderedDescending;// 5 4
            } else {
               return NSOrderedSame;
            }
            
            // 方法三:
            if (obj1.integerValue > obj2.integerValue) {
                return NSOrderedDescending;// 5 4
            } else if (obj1.integerValue < obj2.integerValue) {
                return NSOrderedAscending;// 3 5
            } else {
               return NSOrderedSame;
            }
            
        }];
    
        NSLog(@"对数组进行排序(正序):%@", resultAscending);
        // 对数组进行排序(降序)
        NSArray *resultDescending = [array sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
            NSLog(@"%@ ~ %@", obj1, obj2);
            return [obj2 compare:obj1];
        }];
        NSLog(@"对数组进行排序(降序):%@", resultDescending);
        
        
        // 对数组进行排序(乱序)
        NSArray *resultBreak = [resultAscending sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
            NSLog(@"%@ ~ %@", obj1, obj2);
            if (arc4random_uniform(2) == 0) { // arc4random_uniform会随机返回一个0到上界之间(不含上界)的整数。以2为上界会得到0或1,像投硬币一样
                return [obj2 compare:obj1];// descending
            } else {
                return [obj1 compare:obj2];// ascending
            }
        }];
        NSLog(@"对数组进行排序(乱序):%@", resultBreak);
        
        
        // 对数组进行排序(倒序)
        NSArray *resultReversal = [array sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
            NSLog(@"%@ ~ %@", obj1, obj2);
            return NSOrderedDescending;
        }];
        NSLog(@"对数组进行排序(倒序):%@", resultReversal);
    

    打印结果:

    2020-04-21 16:13:32.239073+0800 TT[7523:642448] 3 ~ 5
    2020-04-21 16:13:32.239212+0800 TT[7523:642448] 5 ~ 4
    2020-04-21 16:13:32.239316+0800 TT[7523:642448] 3 ~ 4
    2020-04-21 16:13:32.239402+0800 TT[7523:642448] 2 ~ 1
    2020-04-21 16:13:32.239532+0800 TT[7523:642448] 1 ~ 4
    2020-04-21 16:13:32.239617+0800 TT[7523:642448] 1 ~ 1
    2020-04-21 16:13:32.239732+0800 TT[7523:642448] 2 ~ 1
    2020-04-21 16:13:32.239842+0800 TT[7523:642448] 2 ~ 4
    2020-04-21 16:13:32.240251+0800 TT[7523:642448] 3 ~ 1
    2020-04-21 16:13:32.240555+0800 TT[7523:642448] 3 ~ 1
    2020-04-21 16:13:32.240817+0800 TT[7523:642448] 3 ~ 2
    2020-04-21 16:13:32.241128+0800 TT[7523:642448] 3 ~ 4
    2020-04-21 16:13:32.241415+0800 TT[7523:642448] 4 ~ 4
    2020-04-21 16:13:32.241758+0800 TT[7523:642448] 5 ~ 4
    2020-04-21 16:13:32.242164+0800 TT[7523:642448] 对数组进行排序(正序):(
        1,
        1,
        2,
        3,
        4,
        4,
        5
    )
    2020-04-21 16:13:32.242530+0800 TT[7523:642448] 3 ~ 5
    2020-04-21 16:13:32.242896+0800 TT[7523:642448] 3 ~ 4
    2020-04-21 16:13:32.243326+0800 TT[7523:642448] 5 ~ 4
    2020-04-21 16:13:32.243801+0800 TT[7523:642448] 2 ~ 1
    2020-04-21 16:13:32.245034+0800 TT[7523:642448] 1 ~ 4
    2020-04-21 16:13:32.245157+0800 TT[7523:642448] 2 ~ 4
    2020-04-21 16:13:32.245255+0800 TT[7523:642448] 2 ~ 1
    2020-04-21 16:13:32.245587+0800 TT[7523:642448] 1 ~ 1
    2020-04-21 16:13:32.245928+0800 TT[7523:642448] 5 ~ 4
    2020-04-21 16:13:32.246260+0800 TT[7523:642448] 4 ~ 4
    2020-04-21 16:13:32.246611+0800 TT[7523:642448] 3 ~ 4
    2020-04-21 16:13:32.246962+0800 TT[7523:642448] 3 ~ 2
    2020-04-21 16:13:32.247345+0800 TT[7523:642448] 对数组进行排序(降序):(
        5,
        4,
        4,
        3,
        2,
        1,
        1
    )
    2020-04-21 16:13:32.247798+0800 TT[7523:642448] 1 ~ 1
    2020-04-21 16:13:32.248112+0800 TT[7523:642448] 1 ~ 2
    2020-04-21 16:13:32.248542+0800 TT[7523:642448] 3 ~ 4
    2020-04-21 16:13:32.249035+0800 TT[7523:642448] 4 ~ 5
    2020-04-21 16:13:32.249276+0800 TT[7523:642448] 3 ~ 5
    2020-04-21 16:13:32.249614+0800 TT[7523:642448] 4 ~ 5
    2020-04-21 16:13:32.249983+0800 TT[7523:642448] 4 ~ 4
    2020-04-21 16:13:32.250339+0800 TT[7523:642448] 1 ~ 3
    2020-04-21 16:13:32.250672+0800 TT[7523:642448] 1 ~ 3
    2020-04-21 16:13:32.251180+0800 TT[7523:642448] 2 ~ 3
    2020-04-21 16:13:32.251723+0800 TT[7523:642448] 2 ~ 5
    2020-04-21 16:13:32.252757+0800 TT[7523:642448] 对数组进行排序(乱序):(
        1,
        1,
        3,
        2,
        5,
        4,
        4
    )
    2020-04-21 16:13:32.252884+0800 TT[7523:642448] 3 ~ 5
    2020-04-21 16:13:32.253037+0800 TT[7523:642448] 3 ~ 4
    2020-04-21 16:13:32.253498+0800 TT[7523:642448] 5 ~ 4
    2020-04-21 16:13:32.253734+0800 TT[7523:642448] 2 ~ 1
    2020-04-21 16:13:32.254113+0800 TT[7523:642448] 1 ~ 4
    2020-04-21 16:13:32.254624+0800 TT[7523:642448] 1 ~ 4
    2020-04-21 16:13:32.254758+0800 TT[7523:642448] 1 ~ 1
    2020-04-21 16:13:32.255501+0800 TT[7523:642448] 4 ~ 4
    2020-04-21 16:13:32.255678+0800 TT[7523:642448] 4 ~ 1
    2020-04-21 16:13:32.256158+0800 TT[7523:642448] 4 ~ 1
    2020-04-21 16:13:32.256340+0800 TT[7523:642448] 4 ~ 2
    2020-04-21 16:13:32.256795+0800 TT[7523:642448] 对数组进行排序(倒序):(
        4,
        1,
        1,
        2,
        4,
        5,
        3
    )
    
    

    相关文章

      网友评论

          本文标题:iOS - sortedArrayUsingComparator

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