由于集合的使用过程中,经常需要对数组进行排序操作,此博客用于总结对在OC中对数组排序的几种方法

1.当数组中存放的是Foundation框架中提供的对象时,直接使用 compare:方法

如:NSString、NSMutableSting等

 

 1         //使用块对数组排序
 2         NSArray* arr = @[@"4",@"3",@"1",@"2"];
 3         NSMutableArray* mutArr = [arr mutableCopy];
 4
 5         //可变数组使用块 sortUsingComparator
 6         [mutArr sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
 7             return [obj1 compare: obj2];
 8         }];
 9         //不可变数组使用Comparator排序
10         NSArray* sorted = [arr sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
11             return [obj2 compare: obj1];
12         }];
13
14         NSLog(@"%@", sorted);
15          NSLog(@"%@", mutArr);
16
17         //使用块对数组进行排序
18         //可变数组使用selector排序
19         NSLog(@"%@", [arr sortedArrayUsingSelector:@selector(compare:)]);

2. 当数组中存放的是自定义对象时,需要自己写排序方法,或使用NSSortDescriptor进行排序

举个例子,自定义一个Student类,生成很多Student对象,放入数组中,对数组进行排序。

2.1 Student类

 1 #import <Foundation/Foundation.h>
 2 @class Grade;
 3 @interface Student : NSObject
 4 @property (copy, nonatomic) NSString* name;
 5 @property (assign, nonatomic) NSInteger age;
 6 @property (copy, nonatomic) NSString* stuNo;
 7 @property (strong, nonatomic) Grade* grade;
 8 - (NSComparisonResult)compare:(Student *)stu;
 9
10 - (NSString *)description;
11
12 @end
13
14 @implementation Student
15 - (NSComparisonResult)compare:(Student *)stu
16 {
17     return [_name compare: [stu name]];
18 }
19 - (NSString *)description
20 {
21     return [NSString stringWithFormat:@"%@", _name];
22 }
23 @end

2.2 排序 (使用自定义方法)

 1 //对自定义对象进行排序
 2         Grade* grade1 = [[Grade alloc] init];
 3         grade1.name = @"1023";
 4         Grade* grade2 = [[Grade alloc] init];
 5         grade2.name = @"1024";
 6
 7         Student* stu1 = [[Student alloc] init];
 8         stu1.name = @"悟空";
 9         stu1.stuNo = @"008";
10         stu1.age = 10;
11         stu1.grade = grade1;
12         Student* stu2 = [[Student alloc] init];
13         stu2.name = @"八戒";
14         stu2.stuNo = @"007";
15         stu2.age = 12;
16         stu2.grade = grade2;
17         Student* stu3 = [[Student alloc] init];
18         stu3.name = @"唐僧";
19         stu3.stuNo = @"009";
20         stu3.age = 14;
21         stu3.grade = grade2;
22         Student* stu4 = [[Student alloc] init];
23         stu4.name = @"玉帝";
24         stu4.stuNo = @"011";
25         stu4.age = 16;
26         stu4.grade = grade1;
27         Student* stu5 = [[Student alloc] init];
28         stu5.name = @"观音";
29         stu5.stuNo = @"112";
30         stu5.age = 14;
31         stu5.grade = grade1;
32
33         NSArray* students = @[stu1, stu2, stu3, stu4,stu5];
34         //对自定义对象排序需要自定义排序方法
35         NSArray* orderedArr = [students sortedArrayUsingSelector:@selector(compare:)];
36 //        NSLog(@"%@", orderedArr);
37         for (Student *new in orderedArr) {
38             NSLog(@"%@", new.name);
39         }

3.使用NSSortDescriptor进行排序,NSSortDescriptor排序更多的用于多条件排序

  使用步骤:

  1>创建NSSortDescriptor对象

1      //使用NSSortDescriptor对象描述某一条属性的比较规则
2         //第一个参数,是要比较的属性所对应的成员变量的名字
3         //第二个参数,指定为YES, 表示为升序,指定为NO,表示为降序
4
5         NSSortDescriptor* desc1 = [[NSSortDescriptor alloc] initWithKey:@"_stuNo" ascending:YES];
6         NSSortDescriptor* desc2 = [[NSSortDescriptor alloc] initWithKey:@"_age" ascending:YES];
7         NSSortDescriptor* desc3 = [[NSSortDescriptor alloc] initWithKey:@"_name" ascending:YES];
8         //【注】NSSortDescriptor 支持路径,_grade为student对象的属性对象,name为_grade的属性
9         NSSortDescriptor* desc4 = [[NSSortDescriptor alloc] initWithKey:@"_grade.name" ascending:YES];

  2>将NSSortDescriptor对象放入数组

1 NSArray* descArr = [NSArray arrayWithObjects: desc4,desc1, desc3, desc2, nil];

  3>需要排序的数组调用  sortedArrayUsingDescriptors:方法进行排序

1 NSArray* sortedByDescriptor = [students sortedArrayUsingDescriptors: descArr];

  实例代码如下:

  Student.h文件

 1 #import <Foundation/Foundation.h>
 2 @class Grade;
 3 @interface Student : NSObject
 4 @property (copy, nonatomic) NSString* name;
 5 @property (assign, nonatomic) NSInteger age;
 6 @property (copy, nonatomic) NSString* stuNo;
 7 @property (strong, nonatomic) Grade* grade;
 8 - (NSComparisonResult)compare:(Student *)stu;
 9
10 - (NSString *)description;
11
12 @end

  Student.m文件

 1 #import "Student.h"
 2
 3 @implementation Student
 4 - (NSComparisonResult)compare:(Student *)stu
 5 {
 6     return [_name compare: [stu name]];
 7 }
 8 - (NSString *)description
 9 {
10     return [NSString stringWithFormat:@"%@", _name];
11 }
12 @end

  Grade.h文件

1 #import <Foundation/Foundation.h>
2
3 @interface Grade : NSObject
4 @property (copy, nonatomic) NSString* name;
5 @end

  Grade.m文件

  

1 #import "Grade.h"
2
3 @implementation Grade
4
5 @end

  主函数:

 1 //对自定义对象进行排序
 2         Grade* grade1 = [[Grade alloc] init];
 3         grade1.name = @"1023";
 4         Grade* grade2 = [[Grade alloc] init];
 5         grade2.name = @"1024";
 6
 7         Student* stu1 = [[Student alloc] init];
 8         stu1.name = @"悟空";
 9         stu1.stuNo = @"008";
10         stu1.age = 10;
11         stu1.grade = grade1;
12         Student* stu2 = [[Student alloc] init];
13         stu2.name = @"八戒";
14         stu2.stuNo = @"007";
15         stu2.age = 12;
16         stu2.grade = grade2;
17         Student* stu3 = [[Student alloc] init];
18         stu3.name = @"唐僧";
19         stu3.stuNo = @"009";
20         stu3.age = 14;
21         stu3.grade = grade2;
22         Student* stu4 = [[Student alloc] init];
23         stu4.name = @"玉帝";
24         stu4.stuNo = @"011";
25         stu4.age = 16;
26         stu4.grade = grade1;
27         Student* stu5 = [[Student alloc] init];
28         stu5.name = @"观音";
29         stu5.stuNo = @"112";
30         stu5.age = 14;
31         stu5.grade = grade1;
32
33         NSArray* students = @[stu1, stu2, stu3, stu4,stu5];
34
35 NSSortDescriptor* desc1 = [[NSSortDescriptor alloc] initWithKey:@"_stuNo" ascending:YES];
36         NSSortDescriptor* desc2 = [[NSSortDescriptor alloc] initWithKey:@"_age" ascending:YES];
37         NSSortDescriptor* desc3 = [[NSSortDescriptor alloc] initWithKey:@"_name" ascending:YES];
38         //可向下传递
39         NSSortDescriptor* desc4 = [[NSSortDescriptor alloc] initWithKey:@"_grade.name" ascending:YES];
40         NSArray* descArr = [NSArray arrayWithObjects: desc4,desc1, desc3, desc2, nil];
41 //        NSArray* descArr = [NSArray arrayWithObjects:desc4, nil];
42         NSArray* sortedByDescriptor = [students sortedArrayUsingDescriptors: descArr];
43
44         for (Student *new in sortedByDescriptor) {
45             NSLog(@"%@", new.grade.name);
46         }

转载于:https://www.cnblogs.com/pretty-guy/p/3996374.html

NSArray 与 NSMutableArray 的排序相关推荐

  1. iOS开发中对NSArray或者NSMutableArray中的内容排序

    原文引用至               http://blog.unieagle.net/2012/05/10/ios开发中对nsarray或者nsmutablearray中的内容排序/ NSMuta ...

  2. NSArray和NSMutableArray对象的使用

    /* 初始化方法:     1.init返回一个空数组      2.initWithArray从已有数组初始化      3.initWithContentsOfFile//从plist文件加载   ...

  3. NSArray和NSMutableArray的常用方法

    /* 初始化方法:     1.init返回一个空数组      2.initWithArray从已有数组初始化      3.initWithContentsOfFile//从plist文件加载   ...

  4. OC中的NSArray和NSMutableArray、NSDictionary和NSMutableDictionary用法

    一:NSArray 和NSMutableArray 1: NSArray:不可变数组 NSArray是OC中使用的数组,只能用来存放OC对象,不能存放非OC对象如基本数据类型 它使不可变的,一旦初始化 ...

  5. NSArray、NSMutableArray和NSMutableDictionary的用法

    转自:http://www.cnblogs.com/wangpei/admin/EditPosts.aspx?opt=1 NSArray是静态的数组,就是它所指向的内容是不可改变的,它指向一段内存区域 ...

  6. iOS NSArray 、NSMutableArray原理揭露

    在iOS开发中,我们在非常非常多的地方用到了数组.而关于数组,有很多需要注意和优化的细节,需要我们潜入到下面,去了解. 阅读<Effective Objective-C 2.0>的原版的时 ...

  7. Foundation 框架 NSArray、NSMutableArray排序

    一.使用selector对数组进行排序(无返回) 数组 book 中包含 AddressCard对象. 1.对数组调用 sortUsingSelector方法 1 -(void) sortByName ...

  8. NSArray和NSMutableArray

    1.不可变数组(NSArray)的操作 1.1.不可变数组的声明,不可变数组生命有很多方式,这里只有最基本的一种 // 声明一个数组 // 数组的元素可以是任意的对象 // 数组中装的是对象的地址 N ...

  9. iOS开发——NSArray中的字典排序

    手头上碰到一个项目,需要给数组中的字典中的一个字段排序,想了想,干脆再字典中增加一个字段,用来记录需要排序字段的第一个字符,用它来作为比较的对象,进行排序. - (void)viewDidLoad { ...

最新文章

  1. Android .9.png图片的处理
  2. linux Figlet 转换字符字
  3. java 打包jar文件以在没有安装JDK或JRE的机子上运行
  4. 超级好用 将html字符串,转化为纯文本
  5. 【数据结构与算法】【算法思想】动态规划
  6. mongoDB高级查询
  7. iOS微信分享在6plus上遇到一个坑
  8. Opengl_19_assimp_1
  9. 【Python实现视频转文字操作】
  10. nas磁盘用什么软件测试,手把手教你从NAS拿点空间当电脑硬盘使用 iSCSI开启网络硬盘共享...
  11. fluent的udf在windows可以编译 linux错误,fluent中udf环境变量设置,简单可行!已试过!...
  12. C语言键盘控制走迷宫小游戏
  13. 初学者须知 常见Web前端开发工具有哪些
  14. win10家庭版解决“管理员已阻止你运行此应用”
  15. 什么是前端、什么是后端
  16. 杜绝企业机密外泄,U-Mail邮件防泄密解决方案
  17. FPGA 视频处理中外部SDRAM的作用
  18. 大学生涯(一)电脑工具篇
  19. 最牛的商业模式,化肥厂利用买化肥送车,一年总利润600多万?
  20. ConfigurationChanged流程梳理(屏幕旋转、语言及字体切换)

热门文章

  1. 速卖通爆款如何打造,爆款的周期有多久?
  2. VC6解决托盘菜单不消失
  3. [X11forword]Display remote application with X11 forword / 远程显示linux服务器GUI
  4. 洛谷精选 - 字符串合集
  5. 读书笔记-JavaScript高级程序设计(1)
  6. 谈谈自己的理解:python中闭包,闭包的实质
  7. sqlservcer行列互转
  8. 统一建模语言UML整理之开篇
  9. 字符串匹配のKMP【专题@AbandonZHANG】
  10. 这个世界并不缺少创意,而是缺少发现