iOS开发

OC中数组排序总结

2018-02-26  本文已影响22人  刘勇虎

过完节回来,感觉很多东西都生疏了。总结一下数组的排序。应该不会太完美,后续添加补充。

OC中的数组排序方法其实不太多,要根据不同的使用场景来使用不同的方法。Foundation框架中一般用到一下几个方法。

1.sortedArrayUsingSelector

此方法一般用于且NSNumber、NSString的基础数据数组排序,默认升序。

举个🌰

        NSArray *array = @[@"1",@"2",@"23",@"123"];
        NSArray *aarray = [array sortedArrayUsingSelector:@selector(compare:)];
        NSLog(@"aarray = %@",aarray);

打印结果

aarray = (
    1,
    123,
    2,
    23
)

2.sortedArrayUsingComparator

这个方法操作空间就比较大了,也是重点,一般用于数组元素对象的属性比较排序,首先看一下该方法的API文档:

NSArray *aarray = [arraysortUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {code}];

文中usingComparator后边,调取的是一个blok,返回的是枚举类型NSComparisonResult,该block决定了数组的排序方式。

两种写法:

A.不调用compare方法

举个🌰

      NSMutableArray *array = [NSMutableArray arrayWithCapacity:10];
        for (NSInteger i = 0; i < 4 ; i ++) {
            Person *per = [[Person alloc]init];
            per.age = arc4random()%10 +1;
            NSLog(@"per =%ld",per.age);
            [array addObject:per];
        }
        
        NSArray *aarray = [array sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
            Person *per1 = obj1;
            Person *per2 = obj2;
            if (per1.age > per2.age) {
//降序
                return NSOrderedAscending;
//升序
//              return NSOrderedDescending; 

            }else if (per1.age < per2.age){
//升序
//                return NSOrderedAscending;
//降序
                  return NSOrderedDescending;
            }else{
                return NSOrderedSame;
            }
        }];
        for (Person *per in aarray) {

            NSLog(@"== %ld",per.age);
        }

打印结果:

2018-02-26 17:48:18.051843+0800 comandLine[2527:1303915] per =2
2018-02-26 17:48:18.052047+0800 comandLine[2527:1303915] per =6
2018-02-26 17:48:18.052060+0800 comandLine[2527:1303915] per =5
2018-02-26 17:48:18.052070+0800 comandLine[2527:1303915] per =5
2018-02-26 17:48:18.052126+0800 comandLine[2527:1303915] == 6
2018-02-26 17:48:18.052137+0800 comandLine[2527:1303915] == 5
2018-02-26 17:48:18.052146+0800 comandLine[2527:1303915] == 5
2018-02-26 17:48:18.052155+0800 comandLine[2527:1303915] == 2
A.调用compare方法

举个🌰

     NSMutableArray *array = [NSMutableArray arrayWithCapacity:10];
        for (NSInteger i = 0; i < 4 ; i ++) {
            Person *per = [[Person alloc]init];
            per.age = arc4random()%10 +1;
            NSLog(@"per =%ld",per.age);
            [array addObject:per];
        }
        
        NSArray *aarray = [array sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
            Person *per1 = obj1;
            Person *per2 = obj2;
//升序
            return [@([per1 age]) compare:@([per2 age])];
  //降序
   //        return -[@([per1 age]) compare:@([per2 age])];      
}];
        for (Person *per in aarray) {

            NSLog(@"== %ld",per.age);
        }

打印结果

2018-02-26 17:50:14.930696+0800 comandLine[2573:1314917] per =9
2018-02-26 17:50:14.930995+0800 comandLine[2573:1314917] per =1
2018-02-26 17:50:14.931013+0800 comandLine[2573:1314917] per =9
2018-02-26 17:50:14.931031+0800 comandLine[2573:1314917] per =10
2018-02-26 17:50:14.931149+0800 comandLine[2573:1314917] == 1
2018-02-26 17:50:14.931161+0800 comandLine[2573:1314917] == 9
2018-02-26 17:50:14.931167+0800 comandLine[2573:1314917] == 9
2018-02-26 17:50:14.931172+0800 comandLine[2573:1314917] == 10

   
   
   

友情链接:

技术博客        简书主页

上一篇下一篇

猜你喜欢

热点阅读