NSArray 与 NSMutableArray 的排序

  由于集合的使用过程中,经常需要对数组进行排序操作,此博客用于总结对在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         }
时间: 2024-08-13 01:52:06

NSArray 与 NSMutableArray 的排序的相关文章

[Objective-C] 008_Foundation框架之NSArray与NSMutableArray

在Cocoa Foundation中NSArray和NSMutableArray 用于对象有序集合,NSArray和NSMutableArray类最大的区别是:NSArray是不可变,NSMutableArray是可变的.它们只能存储Cocoa对象(NSObject对象),如果想保存一些原始的C数据(如:int,float,double,BOOL等),则需要将这些原始的C数据封装NSNumber类型,它们的下标是从0开始,下面是NSArray和NSMutableArray类的一些常用初级操作.

NSArray与NSMutableArray 数组与可变数组的创建和遍历 复习

1.NSArray 是一个父类,NSMUtableArray是其子类,他们构成了OC的数组. 2.NSArray的创建 NSArray * array = [[NSArray alloc]initWithObjects:@"one",@"two",@"three",@"four good",nil];//用对象初始化一个数组,这里是任意四个对象,不一定是字符串.//OC中的数组,不是真正的数组,这是一个链表,nil的作用正是表

关于NSArray的几种排序:

#利用数组的sortedArrayUsingComparator调用 NSComparator  其中NSComparator其实就是一个返回NSComparisonResult的block. typedef NSComparisonResult (^NSComparator)(id obj1, id obj2); 其中obj1.obj2其实是NSArray中的元素 resultArray = [arrayDic <span style="color:#009900;">s

NSArray和NSMutableArray相互转换, 以及常用方法-备

有时候项目中NSArray和NSMutableArray需要相互转化,下面给出相关代码1.NSArray 1.1 转化:NSMutableArray 1 NSMutableArray *mutableArray = [NSMutableArray arrayWithArray:array]; // array为NSMutableArray类型 1.2 常用方法 不可变数组对象: [array count] : 得到这个对象数组的长度. [array objectAtIndex 0]: 传入数组脚

NSArray、NSMutableArray基本用法

郝萌主倾心贡献,尊重作者的劳动成果,请勿转载. 如果文章对您有所帮助,欢迎给作者捐赠,支持郝萌主,捐赠数额随意,重在心意^_^ 我要捐赠: 点击捐赠 Cocos2d-X源码下载:点我传送 游戏官方下载:http://dwz.cn/RwTjl 游戏视频预览:http://dwz.cn/RzHHd 游戏开发博客:http://dwz.cn/RzJzI 游戏源码传送:http://dwz.cn/Nret1 初始化方法: 1.init返回一个空数组 2.initWithArray从已有数组初始化 3.i

Foundation框架(NSArray、NSMutableArray)

第一讲 NSArray.NSMutableArray 一.NSArray 概述:NSArray是OC中的数组类,开发中建议尽量使用NSArray替代C语言中的数组,C语言中数组的弊端,int array[4] = {10, 89, 27, 76};只能存放一种类型的数据.(类型必须一致),不能很方便地动态添加数组元素.不能很方便地动态删除数组元素(长度固定). 使用注意:只能存放任意OC对象, 并且是有顺序的,不能存储非OC对象, 比如int\float\double\char\enum\str

Foundation框架中NSArray和NSMutableArray

=================== NSArray ====================(不可变数组,一旦建立就不能被更改) Ordered collection of objects. Immutable(you cannot add or remove objects to it once it’s created)Important methods:+ (id)arrayWithObjects:(id)firstObject, ...;    // nil terminated -

NSArray,NSMutableArray的一些常用方法

不可变数组 --NSArray 常用的初始化一个数组:       NSArray *array1 = [[NSArray alloc] init];       NSArray *array2 = [NSArray array]; //对于上述两个方法,在NSArray中用的很少,因为这是一个空数组,它的值是不可改变的,意味着一旦创建,就永远为空       NSArray *array3 = [NSArray arrayWithObjects:[NSNumber numberWithInt:

Objective-C 下用 NSArray 和 NSMutableArray 定义二维数组跟多维数

Objective-C 下用 NSArray 和 NSMutableArray 定义二维数组跟多维数组 目录 问题描述 Objective-C 中的数组对像 NSArray 和 NSMutableArray 简介 二维数组:嵌套两次的 NSMutableArray 多维数组:嵌套多次的 NSMutableArray 问题描述 说实话,不太习惯面向对象编程,所以在操作 Objective-C 中数组对象时,总是忍不住跟 C 中的数组做比较,正好要建立一个二维数组,需要根据二维数组的下标来做相应的设