登录 用户中心() [退出] 后台管理 注册
   
您的位置: 首页 >> SoftHub关联区 >> 主题: [ios]NSArray和NSMutableArray内容的排序     [回主站]     [分站链接]
[ios]NSArray和NSMutableArray内容的排序
clq
浏览(259) - 2019-08-01 15:36:03 发表 编辑

关键字: ios_code

[ios]NSArray和NSMutableArray内容的排序

https://www.jianshu.com/p/84804196972d

NSArray和NSMutableArray内容的排序
96 WallFacer
2016.09.16 23:26* 字数 594 阅读 9133评论 0喜欢 3
NSArray和NSMutableArray可以对其中的元素进行排序,有多种方法。

1.可以直接使用系统定义的函数进行排序,要保证比较元素本身具有比较方法,比如NSString类具有(compare:)(caseInsensitiveCompare:)方法。
<pre><code>
NSArray *array = @[@"abc",@"acc",@"adc",@"Abc",@"111"];

//
compare:区分字母大小写

NSArray *testArr = [array sortedArrayUsingSelector:@selector(compare:)];

//caseInsensitiveCompare不区分大小写

NSArray *testArr2 = [array sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];

NSLog(@"%@",[testArr description]);
</code></pre>

其中@selector中compare:方法会区分大小写,而 caseInsensitiveCompare: 不区分大小写。

对于NSMutableArray,由于是可变类型,可以直接调用方法进行排序改变自己,不需要另外创建一个数组来存贮。
代码如下

NSMutableArray *array = [@[@"acc",@"adc",@"bac",@"111",@"Abc",@"Bac"] mutableCopy];
[array sortUsingSelector:@selector(compare:)];

2.使用array sortedArrayWithOptions: usingComparator: 方法进行排序
参数解释:
optoins :
是枚举值NSSortOptions
(NSSortStable,NSSortConcurrent)
NSSortStable 更稳定,但是效率低,比较时是串行操作。
NSSortConcurrent 比较时是并行操作,效率高,适合排序大规模数据

解释参考blog:http://blog.csdn.net/miscellaner/article/details/41870183

comparator :
是一个block,需要返回NSComparisonResult类型的值,NSComparisonResult也是一个枚举值,用来比较大小时用到。这个block里需要做的事就是你按照你的标准比较你的NSArray里的对象,根据你的标准返回一个NSComparisonResult的值,下面是示例:

NSArray *array = @[@"acc",@"adc",@"bac",@"111",@"Abc",@"Bac"];
NSArray *testArr = [array sortedArrayWithOptions:NSSortStable usingComparator:
^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
            NSString *obj11 = obj1;
            NSString *obj22 = obj2;
            NSComparisonResult result = [obj11 compare:obj22];           
            return result;
        }];

为进一步说明NSComparisonResult的功能,下面使用一个NSNumber的数组来说明

NSArray *array = @[@1,@2,@3,@1,@2,@5,@4];

NSArray *testArr = [array sortedArrayWithOptions:NSSortStable usingComparator:
^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
            int value1 = [obj1 intValue];
            int value2 = [obj2 intValue];
            if (value1 > value2) {
                return NSOrderedDescending;
            }else if (value1 == value2){
                return NSOrderedSame;
            }else{
                return NSOrderedAscending;
            }
        }];

3.sortedArrayUsingComparator 方法,与第二种方法几乎一样,没有第一个参数,不再赘述。

4.sortedArrayUsingDescriptors方法,这个方法可以和第二种方法一起理解。下面先贴代码。

NSArray *array = @[@1,@2,@3,@1,@2,@5,@4];
        NSSortDescriptor *sortDescripttor1 = [NSSortDescriptor sortDescriptorWithKey:@"intValue" ascending:YES];
        NSArray *testArr = [array sortedArrayUsingDescriptors:@[sortDescripttor]];

其中NSSortDescriptor用来描述比较标准
有三种创建方法

NSSortDescriptor *sortDescripttor1 = [NSSortDescriptor sortDescriptorWithKey:@"intValue" ascending:YES];

NSSortDescriptor *sortDescripttor2 = [NSSortDescriptor sortDescriptorWithKey:@"intValue" ascending:YES selector:@selector(compare:)];
       
NSSortDescriptor *sortDescripttor3 = [NSSortDescriptor sortDescriptorWithKey:@"intValue" ascending:YES comparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
            return [obj1 compare:obj2];
        }];

参数解释:
<b>sortDescriptorWithKey :</b>
使用kvc取出对象里对应的值.
<b>ascending:</b>
是否升序.
<b>selector:</b>
排序方法
<b>compare:</b>
排序block
这三种创建方法创建的对象类似于<b>sortedArrayUsingComparator</b>中block的作用。
可以对自定义类进行排序。

5.<b>sortedArrayUsingFunction</b>方法

NSInteger intSort(id num1, id num2, void *context)
{
    int v1 = [num1 intValue];
    int v2 = [num2 intValue];
    if (v1 < v2)
        return NSOrderedAscending;
    else if (v1 > v2)
        return NSOrderedDescending;
    else
        return NSOrderedSame;
}

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
       
        NSArray *array = @[@1,@2,@3,@1,@2,@5,@4];
       
        NSArray *testArr = [array sortedArrayUsingFunction:intSort context:nil];
        NSLog(@"%@",[testArr description]);

    }
   
    return 0;
}

就是定义一个方法返回两两比较中应该返回的值,从而达到排序的目的。此外还有

sortedArrayUsingFunction:  context: hint:];

尚不清楚hint的用处,待填。
参考blog:http://www.cnblogs.com/xiaobaizhu/archive/2013/06/05/3119983.html



总数:0 页次:1/0 首页 尾页  
总数:0 页次:1/0 首页 尾页  


所在合集/目录



发表评论:
文本/html模式切换 插入图片 文本/html模式切换


附件:



NEWBT官方QQ群1: 276678893
可求档连环画,漫画;询问文本处理大师等软件使用技巧;求档softhub软件下载及使用技巧.
但不可"开车",严禁国家敏感话题,不可求档涉及版权的文档软件.
验证问题说明申请入群原因即可.

Copyright © 2005-2020 clq, All Rights Reserved
版权所有
桂ICP备15002303号-1