zoukankan      html  css  js  c++  java
  • 不可变数组或者可变数组进行排序

    #import <Foundation/Foundation.h>

    NSInteger myCompare(id obj1,id obj2,void *context)

    {

        //不分大小写进行升序排序

        //return [obj1 caseInsensitiveCompare:obj2];

       return -[obj1 caseInsensitiveCompare:obj2];

    }

    int main(int argc, const char * argv[])

    {

        @autoreleasepool {

            

            

            NSArray *array=@[@"one", @"Two", @"Three", @"Four"];

            NSMutableArray *mutableArray=[NSMutableArrayarrayWithArray:array];

            //1不可变数组通过自定义比较规则来比较字符串

           NSArray *result= [array sortedArrayUsingFunction:myComparecontext:nil];

            NSLog(@"%@ ",result);

            //类提供compare方法不想改变数组顺序

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

            NSLog(@"%@ ",array);

            

            //2直接在数组内排序

            [mutableArray sortUsingFunction:myCompare context:nil];

            NSLog(@"%@ ",mutableArray);

            

        }

        return 0;

    }

     

  • 相关阅读:
    多线程(5)async&await
    多线程(4)Task
    多线程(3)ThreadPool
    多线程(2)Thread
    多线程(1)认识多线程
    泛型
    反射(4)反射性能问题:直接调用vs反射调用
    反射(3)反射应用:一个插件项目
    反射(2)使用反射
    反射(1)认识反射
  • 原文地址:https://www.cnblogs.com/thbbsky/p/3751303.html
Copyright © 2011-2022 走看看