由于集合的使用过程中,经常需要对数组进行排序操作,此博客用于总结对在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. 限定只能处理"A仓"和"B仓"入库

    应用 Oracle Inventory 层 Level Function 函数名 Funcgtion Name INV_INVTTMTX_MISC 表单名 Form Name INVTTMTX 说明 ...

  2. VC中利用多线程技术实现线程之间的通信

    当前流行的Windows操作系统能同时运行几个程序(独立运行的程序又称之为进程),对于同一个程序,它又可以分成若干个独立的执行流,我们称之为线程,线程提供了多任务处理的能力.用进程和线程的观点来研究软 ...

  3. iOS 数组里面取字典的值

    NSArray *arrData = @[@"1",@"2",@"3",@"4"]; NSArray *arrKey = ...

  4. transition与visibility与display

    http://www.zhangxinxu.com/wordpress/2013/05/transition-visibility-show-hide/ 术语解释是: visibility: 离散步骤 ...

  5. css两个紧挨着的css选择器修饰同一个元素

    #status, .commands{ height: 25px; line-height: 25px;}.upload .commands{ float: right;}.hidden{ displ ...

  6. cron 定时任务

    cron 是linux下的定时任务: M H D m d cmd.  这是一种cron文件格式.   M: 分钟(0-59). H:小时(0-23). D:天(1-31). m: 月(1-12). d ...

  7. Jquer学习之jQuery(function(){})与(function(){})(jQuery)之间的区别

    Jquery是优秀的Javascrīpt框架.我们现在来讨论下在 Jquery 中两个页面载入后执行的函数. $(document).ready(function(){ // 在这里写你的代码... ...

  8. English - 英文写作中的最常见“十大句式”

    英文写作中的最常见“十大句式” from 小木虫论坛 一.否定句 许多否定句不含not的否定结构.如果论文作者能正确使用他们,就会增加写作的闪光点,使文章显得生动活泼. 1.Instead of in ...

  9. ThinkPHP第二十六天(JQuery操作select,SESSION和COOKIE)

    1.JQuery操作select,假设<select id="my"> A:双击选项<option>事件,应该是select的dbclick事件. B:获得 ...

  10. ETL工具之ODI

    ETL工具之ODI         到目前为止,Oracle的ETL工具包括两种,分别是Oracle Warehouse Builder(OWB)和Oracle Data Integrator(ODI ...