由于集合的使用过程中,经常需要对数组进行排序操作,此博客用于总结对在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. surface 其实是UEFI与BIOS并存,借用官网的进入方法(少有更改)

    surface 其实是UEFI与BIOS并存,借用官网的进入方法(少有更改) 第一种: 1.       Swipe in from the right edge of the screen, and ...

  2. 如何解决”无法将类型为“System.DateTime”的对象强制转换为类型“System.String”。“

    字段Time在数据库中为datetime类型 dr.GetString(3).ToString() dr.GetString(3).ToString() => dr.GetDateTime(3) ...

  3. android ViewHolder 使用

    android中使用ListView   ExpandableListView  数据适配器adapter很多都是自己定义,自己定义数据适配器时,要重写getView.重写getView为了不让每次调 ...

  4. 有哪些适合学生参与的 C++,网络编程方面的开源项目?

    有哪些适合学生参与的 C++,网络编程方面的开源项目?   Tinyhttpd是一个超轻量型Http Server,使用C语言开发,全部代码只有502行(包括注释),附带一个简单的Client,可以通 ...

  5. Mongodb数据库命令端经常使用操作

    数据库基本命令操作 数据库经常使用命令 1.Help查看命令提示 help db.help(); db.yourColl.help(); db.youColl.find().help(); rs.he ...

  6. Unity3d 要点板书

    WWW.unity3d.com.cn Unity Project  unity的项目文件/专案 Scene  unity的场景文件 Scene 场景视窗 Game 预览视窗 H... 物件视窗 Pro ...

  7. 配置tomcat的https通信(单向认证)

    1.首先用jdk带的工具生成证书库 打开cmd命令行窗口,cd 到tomcat安装目录的bin下面执行 keytool -v -genkey -alias tomcat -keyalg RSA -ke ...

  8. VBA 简单调试

    在中断模式下(ctrl+Break键),可以做: 1.执行    工具----选项----编辑器----勾选“自动显示数据提示” 则当用鼠标悬停在变量或表达式上时,会出现提示窗口,显示其名称和值! 2 ...

  9. asp.net验证码及怎么获取里面的数值(整合)

    一.ASP.Net的验证码的作用 对于一个预防攻击的web表单来讲,验证码通常是一个常见的措施.因为如果对于一些public区域的页面内容来讲,譬如一个登录表单,如果没有必要的安全措施,很可能遭到模拟 ...

  10. English - 定冠词和不定冠词(a an the) 的区别

    不定冠词表示泛指,定冠词表示特指. 不定冠词a (an)与数词one 同源,是"一个"的意思.a用于辅音音素前,一般读作[e],而an则用于元音音素前,一般读做[en]. 1) 表 ...