美文网首页
iOS NSArray冒泡排序方法与OC自带方法

iOS NSArray冒泡排序方法与OC自带方法

作者: JohnayXiao | 来源:发表于2019-07-09 10:41 被阅读0次
NSMutableArray *arr = @[@1, @3, @2, @0, @9, @8, @7, @5, @6, @4].mutableCopy;
   
   NSInteger count = arr.count;
   
   for (int i = 0; i < count - 1; i++) {
       
       for (int j = i + 1; j < count; j++) {
           
           if ([arr[i] compare:arr[j]] == NSOrderedDescending) {
               
               NSObject *temp = arr[i];
               arr[i] = arr[j];
               arr[j] = temp;
           }
       }
   }
   
   NSLog(@"%@", arr);
NSArray *arr = @[@1, @3, @2, @0, @9, @8, @7, @5, @6, @4];

NSArray *sortedArr = [arr sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        
        return [obj1 compare:obj2];
    }];
    
    NSLog(@"Arr->%@", arr);
    NSLog(@"sortedArr->%@", sortedArr);
    
    NSMutableArray *arrM = [NSMutableArray arrayWithArray:arr];
    [arrM sortUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        
        return [obj2 compare:obj1];
    }];
    NSLog(@"ArrM->%@", arrM);

相关文章

网友评论

      本文标题:iOS NSArray冒泡排序方法与OC自带方法

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