由于集合的使用过程中,经常需要对数组进行排序操作,此博客用于总结对在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 的排序的更多相关文章

  1. iOS阶段学习第15天笔记(NSArray与NSMutableArray 数组)

    iOS学习(OC语言)知识点整理 一.OC中的数组 1)数组:也是一个对象,数组中存放的是对象的地址,可以存放任意类型对象的地址,只能是对象不能是具体的数值,数组是有序的,      可以存放重复的元 ...

  2. NSArray、NSMutableArray基本用法

    NSArray.NSMutableArray基本用法 一.基本操作 初始化方法:1.init返回一个空数组 2.initWithArray从已有数组初始化 3.initWithContentsOfFi ...

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

    有时候项目中NSArray和NSMutableArray需要相互转化,下面给出相关代码1.NSArray 1.1 转化:NSMutableArray 1 NSMutableArray *mutable ...

  4. NSArray和NSMutableArray的常用方法 (转)

    NSArray和NSMutableArray的常用方法 (转) (2013-09-06 15:13:46) 标签: it 分类: ios编程 初始化方法:   1.init返回一个空数组    2.i ...

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

    在Cocoa Foundation中NSArray和NSMutableArray 用于对象有序集合,NSArray和NSMutableArray类最大的区别是:NSArray是不可变,NSMutabl ...

  6. IOS数组NSArray与NSMutableArray知识点

    此文是对数组NSArray与NSMutableArray知识点的总结,主要是一些常见的操作,别外一些操作见其相应的文档,下面的代码部分还运用的第三方插件BlocksKit相结合: a:Foundati ...

  7. NSArray和NSMutableArray

    //1. NSArray EOItems *eOItems = [[EOItems alloc] init]; eOItems.ID = [NSNumber numberWithInt:]; NSAr ...

  8. 15.Object-C--浅谈Foundation框架OC数组NSArray与NSMutableArray

    昨天总结了一下NSString与NSMutableString,今天我在这里总结一下NSArray与NSMutableArray. NSArray数组是:不可变数组. nil 是数组元素结束的标记.O ...

  9. Object-c学习之路八(NSArray(数组)遍历和排序)

    今天学习了NSArray的遍历和排序,现在在这里做一下总结: 遍历现在实现了四中方法: 排序大概有三中方法:(代码中都有注释) 关于对象的排序还是以Student和Book为例 每个Student持有 ...

随机推荐

  1. MYSQL 巧用count,sum进行统计数据

    SELECT a.user,count(b.order_id) as subcount,sum(if(b.verifysta='Y',1,0)) as passcount FROM vicidial_ ...

  2. Unix/Linux环境C编程入门教程(17) Gentoo LinuxCCPP开发环境搭建

    1. Gentoo Linux是一套通用的.快捷的.完全免费的Linux发行,它面向开发人员和网络职业人员.与其他发行不同的是,Gentoo Linux拥有一套先进的包管理系统叫作Portage.在B ...

  3. Unix/Linux环境C编程入门教程(14) Mandriva LinuxCCPP开发环境搭建

    1. Mandriva是目前全球最优秀的Linux发行版之一,稳居于linux排行榜第一梯队. Mandriva公司现在仍然是 这个时候mandriva Linux系统安装完成,基于Mandriva的 ...

  4. 汇编程序hello world

    我们用C,C++,Java,C#等这样一些高级语言时一般会用到一个集成开发环境,啥编译链接之类的操作都集成到一起了,IDE给你自动完成了.随便点几下按钮就编译好运行起来了. 那假如是写了几行汇编代码该 ...

  5. lucene3.6笔记添加搜索功能

    lucene为程序添加搜索功能,此功能基于已创建好的文档的索引之上.这里我已经为一些文档建立了索引,并保存到硬盘上.下面开始针对这些索引,添加搜索功能. 1.简单的TermQuery搜索 Java代码 ...

  6. java自己主动生成验证码

    代码结构: web.xml <? xml version="1.0" encoding="UTF-8"?> <web-app version= ...

  7. 如何在Objective-C中实现链式语法?

    在接触到开源项目 Masonry 后,里面的布局约束的链式写法让我颇感兴趣,就像下面这样: 1 2 3 4 5 6 7 8 UIEdgeInsets padding = UIEdgeInsetsMak ...

  8. How To Set Dark Theme in Visual Studio 2010

    Want to use the visual studio color theme editor to set the dark theme or other themes? Below shows ...

  9. Java I/O流操作(二)---缓冲流[转]

    转自:http://blog.csdn.net/johnny901114/article/details/8710403 一.BufferWriter类 IO的缓冲区的存在就是为了提高效率,把要操作的 ...

  10. npapi加载失败的几个原因

    本文只讨论加载失败的原因,不复述npapi的使用教程 1. 资源文件是否加上 MIMEType命名的id,和html中的<object>的标签是否对应 如果不相同加载必然失败: 2. 注册 ...