zoukankan      html  css  js  c++  java
  • iosOC/C不可变数组排序

    //1.回顾C数组排序

            int a[6] = {1,4,3,5,6,2};

            //选择

            for (int i =0; i<6-1; i++) {

                for (int j = i+1; j<6;j++) {

                    if(a[i]>a[j]){

                        int tmp = a[i];

                        a[i] = a[j];

                        a[j] = tmp;

                    }

                }

            }

            for(int i=0;i<6;i++){

                printf("a[%d] = %d ",i,a[i]);

            }

          

        //NSArray 排序

            NSArray * array = @[@"1",@"4",@"5",@"7",@"2",@"9"];

         //当前放返回值是数组,这个排序是系统默认提供,遵从从小大 具有局限性 不方便扩展 不会经常使用

        [array sortedArrayUsingSelector:@selector(compare:)];

    //    [array sortedArrayWithOptions:<#(NSSortOptions)#> usingComparator:<#^NSComparisonResult(id obj1, id obj2)cmptr#>]

    //      [array sortedArrayUsingFunction:<#(NSInteger (*)(__strong id, __strong id, void *))#> context:<#(void *)#>]

           

            NSMutableArray * arr2 = [[NSMutableArray alloc]initWithArray:array];

            

            for (int i = 0; i<[arr2 count]-1; i++) {

                for (int j = i+1; j<[arr2 count]; j++) {

                if(([arr2[i] intValue])>([arr2[j] intValue])){

                    NSString * str = arr2[i];

                    arr2[i] = arr2[j];

                    arr2[j] = str;

                    }

                }

            }

            NSLog(@"%@",arr2);

  • 相关阅读:
    js中const,var,let区别与用法
    poi excel 导出
    spring 实体类 date类型字段处理
    mysql 1449 : The user specified as a definer ('root'@'%') does not exist
    pjax学习
    上传文件 connection reset
    mysql连接问题
    Scala Actor Model
    Scala 隐式转换
    Scala Trait+Match+Case class+偏函数
  • 原文地址:https://www.cnblogs.com/sunfuyou/p/5900835.html
Copyright © 2011-2022 走看看