NSArray 与 NSMutableArray 的排序
由于集合的使用过程中,经常需要对数组进行排序操作,此博客用于总结对在OC中对数组排序的几种方法
1.当数组中存放的是Foundation框架中提供的对象时,直接使用 compare:方法
如:NSString、NSMutableSting等
//使用块对数组排序
NSArray* arr = @[@"",@"",@"",@""];
NSMutableArray* mutArr = [arr mutableCopy]; //可变数组使用块 sortUsingComparator
[mutArr sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [obj1 compare: obj2];
}];
//不可变数组使用Comparator排序
NSArray* sorted = [arr sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [obj2 compare: obj1];
}]; NSLog(@"%@", sorted);
NSLog(@"%@", mutArr); //使用块对数组进行排序
//可变数组使用selector排序
NSLog(@"%@", [arr sortedArrayUsingSelector:@selector(compare:)]);
2. 当数组中存放的是自定义对象时,需要自己写排序方法,或使用NSSortDescriptor进行排序
举个例子,自定义一个Student类,生成很多Student对象,放入数组中,对数组进行排序。
2.1 Student类
#import <Foundation/Foundation.h>
@class Grade;
@interface Student : NSObject
@property (copy, nonatomic) NSString* name;
@property (assign, nonatomic) NSInteger age;
@property (copy, nonatomic) NSString* stuNo;
@property (strong, nonatomic) Grade* grade;
- (NSComparisonResult)compare:(Student *)stu; - (NSString *)description; @end @implementation Student
- (NSComparisonResult)compare:(Student *)stu
{
return [_name compare: [stu name]];
}
- (NSString *)description
{
return [NSString stringWithFormat:@"%@", _name];
}
@end
2.2 排序 (使用自定义方法)
//对自定义对象进行排序
Grade* grade1 = [[Grade alloc] init];
grade1.name = @"";
Grade* grade2 = [[Grade alloc] init];
grade2.name = @""; Student* stu1 = [[Student alloc] init];
stu1.name = @"悟空";
stu1.stuNo = @"";
stu1.age = ;
stu1.grade = grade1;
Student* stu2 = [[Student alloc] init];
stu2.name = @"八戒";
stu2.stuNo = @"";
stu2.age = ;
stu2.grade = grade2;
Student* stu3 = [[Student alloc] init];
stu3.name = @"唐僧";
stu3.stuNo = @"";
stu3.age = ;
stu3.grade = grade2;
Student* stu4 = [[Student alloc] init];
stu4.name = @"玉帝";
stu4.stuNo = @"";
stu4.age = ;
stu4.grade = grade1;
Student* stu5 = [[Student alloc] init];
stu5.name = @"观音";
stu5.stuNo = @"";
stu5.age = ;
stu5.grade = grade1; NSArray* students = @[stu1, stu2, stu3, stu4,stu5];
//对自定义对象排序需要自定义排序方法
NSArray* orderedArr = [students sortedArrayUsingSelector:@selector(compare:)];
// NSLog(@"%@", orderedArr);
for (Student *new in orderedArr) {
NSLog(@"%@", new.name);
}
3.使用NSSortDescriptor进行排序,NSSortDescriptor排序更多的用于多条件排序
使用步骤:
1>创建NSSortDescriptor对象
//使用NSSortDescriptor对象描述某一条属性的比较规则
//第一个参数,是要比较的属性所对应的成员变量的名字
//第二个参数,指定为YES, 表示为升序,指定为NO,表示为降序 NSSortDescriptor* desc1 = [[NSSortDescriptor alloc] initWithKey:@"_stuNo" ascending:YES];
NSSortDescriptor* desc2 = [[NSSortDescriptor alloc] initWithKey:@"_age" ascending:YES];
NSSortDescriptor* desc3 = [[NSSortDescriptor alloc] initWithKey:@"_name" ascending:YES];
//【注】NSSortDescriptor 支持路径,_grade为student对象的属性对象,name为_grade的属性
NSSortDescriptor* desc4 = [[NSSortDescriptor alloc] initWithKey:@"_grade.name" ascending:YES];
2>将NSSortDescriptor对象放入数组
NSArray* descArr = [NSArray arrayWithObjects: desc4,desc1, desc3, desc2, nil];
3>需要排序的数组调用 sortedArrayUsingDescriptors:方法进行排序
NSArray* sortedByDescriptor = [students sortedArrayUsingDescriptors: descArr];
实例代码如下:
Student.h文件
#import <Foundation/Foundation.h>
@class Grade;
@interface Student : NSObject
@property (copy, nonatomic) NSString* name;
@property (assign, nonatomic) NSInteger age;
@property (copy, nonatomic) NSString* stuNo;
@property (strong, nonatomic) Grade* grade;
- (NSComparisonResult)compare:(Student *)stu; - (NSString *)description; @end
Student.m文件
#import "Student.h" @implementation Student
- (NSComparisonResult)compare:(Student *)stu
{
return [_name compare: [stu name]];
}
- (NSString *)description
{
return [NSString stringWithFormat:@"%@", _name];
}
@end
Grade.h文件
#import <Foundation/Foundation.h> @interface Grade : NSObject
@property (copy, nonatomic) NSString* name;
@end
Grade.m文件
#import "Grade.h" @implementation Grade @end
主函数:
//对自定义对象进行排序
Grade* grade1 = [[Grade alloc] init];
grade1.name = @"";
Grade* grade2 = [[Grade alloc] init];
grade2.name = @""; Student* stu1 = [[Student alloc] init];
stu1.name = @"悟空";
stu1.stuNo = @"";
stu1.age = ;
stu1.grade = grade1;
Student* stu2 = [[Student alloc] init];
stu2.name = @"八戒";
stu2.stuNo = @"";
stu2.age = ;
stu2.grade = grade2;
Student* stu3 = [[Student alloc] init];
stu3.name = @"唐僧";
stu3.stuNo = @"";
stu3.age = ;
stu3.grade = grade2;
Student* stu4 = [[Student alloc] init];
stu4.name = @"玉帝";
stu4.stuNo = @"";
stu4.age = ;
stu4.grade = grade1;
Student* stu5 = [[Student alloc] init];
stu5.name = @"观音";
stu5.stuNo = @"";
stu5.age = ;
stu5.grade = grade1; NSArray* students = @[stu1, stu2, stu3, stu4,stu5]; NSSortDescriptor* desc1 = [[NSSortDescriptor alloc] initWithKey:@"_stuNo" ascending:YES];
NSSortDescriptor* desc2 = [[NSSortDescriptor alloc] initWithKey:@"_age" ascending:YES];
NSSortDescriptor* desc3 = [[NSSortDescriptor alloc] initWithKey:@"_name" ascending:YES];
//可向下传递
NSSortDescriptor* desc4 = [[NSSortDescriptor alloc] initWithKey:@"_grade.name" ascending:YES];
NSArray* descArr = [NSArray arrayWithObjects: desc4,desc1, desc3, desc2, nil];
// NSArray* descArr = [NSArray arrayWithObjects:desc4, nil];
NSArray* sortedByDescriptor = [students sortedArrayUsingDescriptors: descArr]; for (Student *new in sortedByDescriptor) {
NSLog(@"%@", new.grade.name);
}
NSArray 与 NSMutableArray 的排序的更多相关文章
- iOS阶段学习第15天笔记(NSArray与NSMutableArray 数组)
iOS学习(OC语言)知识点整理 一.OC中的数组 1)数组:也是一个对象,数组中存放的是对象的地址,可以存放任意类型对象的地址,只能是对象不能是具体的数值,数组是有序的, 可以存放重复的元 ...
- NSArray、NSMutableArray基本用法
NSArray.NSMutableArray基本用法 一.基本操作 初始化方法:1.init返回一个空数组 2.initWithArray从已有数组初始化 3.initWithContentsOfFi ...
- NSArray和NSMutableArray相互转换, 以及常用方法-备
有时候项目中NSArray和NSMutableArray需要相互转化,下面给出相关代码1.NSArray 1.1 转化:NSMutableArray 1 NSMutableArray *mutable ...
- NSArray和NSMutableArray的常用方法 (转)
NSArray和NSMutableArray的常用方法 (转) (2013-09-06 15:13:46) 标签: it 分类: ios编程 初始化方法: 1.init返回一个空数组 2.i ...
- [Objective-C] 008_Foundation框架之NSArray与NSMutableArray
在Cocoa Foundation中NSArray和NSMutableArray 用于对象有序集合,NSArray和NSMutableArray类最大的区别是:NSArray是不可变,NSMutabl ...
- IOS数组NSArray与NSMutableArray知识点
此文是对数组NSArray与NSMutableArray知识点的总结,主要是一些常见的操作,别外一些操作见其相应的文档,下面的代码部分还运用的第三方插件BlocksKit相结合: a:Foundati ...
- NSArray和NSMutableArray
//1. NSArray EOItems *eOItems = [[EOItems alloc] init]; eOItems.ID = [NSNumber numberWithInt:]; NSAr ...
- 15.Object-C--浅谈Foundation框架OC数组NSArray与NSMutableArray
昨天总结了一下NSString与NSMutableString,今天我在这里总结一下NSArray与NSMutableArray. NSArray数组是:不可变数组. nil 是数组元素结束的标记.O ...
- Object-c学习之路八(NSArray(数组)遍历和排序)
今天学习了NSArray的遍历和排序,现在在这里做一下总结: 遍历现在实现了四中方法: 排序大概有三中方法:(代码中都有注释) 关于对象的排序还是以Student和Book为例 每个Student持有 ...
随机推荐
- [Drools]JAVA规则引擎 -- Drools
Drools是一个基于Java的规则引擎,开源的,可以将复杂多变的规则从硬编码中解放出来,以规则脚本的形式存放在文件中,使得规则的变更不需要修正代码重启机器就可以立即在线上环境生效. 本文所使用的de ...
- 柯南君:看大数据时代下的IT架构(8)消息队列之RabbitMQ--案例(topic起航)
二.Topic(主题) (using the Java client) 上一篇文章中,我们进步改良了我们的日志系统.我们使用direct类型转发器,使得接收者有能力进行选择性的接收日志,,而非fano ...
- Python开发技术详解(视频+源码+文档)
Python, 是一种面向对象.直译式计算机程序设计语言.Python语法简捷而清晰,具有丰富和强大的类库.它常被昵称为胶水语言,它能够很轻松的把用其他语言制作的各种模块(尤其是C/C++)轻松地联结 ...
- 三点顺序_nyoj_68(计算几何).java
三点顺序 时间限制: 1000 ms | 内存限制: 65535 KB 难度: 3 描述 现在给你不共线的三个点A,B,C的坐标,它们一定能组成一个三角形,现在让你判断A,B,C是顺时针给出的 ...
- 链表-Add Two Numbers
第一版代码(很挫很罗嗦,不过是第一次做,记录一下成长的脚步!继续努力!) /*struct ListNode { int val; struct ListNode *next; };*/ typede ...
- java--jsp+ssh+select动态结合数据和选择(解)
在三层体系结构和jsp合并项目,如何实现select动态绑定数据和动态选择指定的行?让我们来看看下面的: 1.首先定义一个Bean分类.它用于实例select的结合数据中的每一个id和name: pu ...
- English - refer to...和refer to...as
refer to...和refer to...as...本来就是refer的两个固定搭配,这个只能讲讲后两者用法,剩下的就是单独的refer的用法了. 1. refer to sb/sth 指的是/提 ...
- AppDelegate关于应用程序挂起、复原与终止的代理方法
AppDelegate关于应用程序挂起.复原与终止的代理方法: 首次运行: - (BOOL)application:(UIApplication *)application didFinishLaun ...
- 使用 UML 进行业务建模:理解业务用例与系统用例的相似和不同之处
使用 UML 进行业务建模:理解业务用例与系统用例的相似和不同之处 作者:Arthur V. English 出处:IBM 本文内容包括: 背景 业务用例模型与系统用例模型有什么相似之处? 业 ...
- BZOJ 2762: [JLOI2011]不等式组( 平衡树 )
对不等式变形..然后就是维护一些数, 随便找个数据结构都能写吧....用double感觉会有精度误差, 分类讨论把<改成<=了很久后弃疗了, 自己写了个分数体....然后速度就被完爆了.. ...