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

    Pearson相关系数 考察两个事物(在数据里我们称之为变量)之间的相关程度,简单来说就是衡量两个数据集合是否在一条线上面.其计算公式为: 或或 N表示变量取值的个数. 相关系数r的值介于–1与+1之 ...

  2. iOS 4.2 SDK安装

    iOS 4.2 SDK安装 ◆系统版本:10.6.5 ◆硬件配置: ◆10G空间,3.5G内存,显卡:GMA950 Xcode 3.2.5和iOS 4.2 SDK下载:http://developer ...

  3. 排序方法之标准库中的快排 qsort ()函数

    C标准库qsort()函数的用法(快排) 使用快速排序例程进行排序 头文件:stdlib.h 用 法: void qsort(void *base, int  nelem, int  width, i ...

  4. Android 4.2启动代码分析(一)

    Android系统启动过程分析 Android系统的框架架构图如下(来自网上):   Linux内核启动之后----->就到Android的Init进程 ----->进而启动Android ...

  5. STMP发送邮件被当垃圾邮件处理的解决方法

    昨天使用了.Net通过smtp发送邮件的方式发送了一封邮件到自己的QQ邮箱,但是发送成功后并没有提示邮箱收到新的邮件,而且去收件箱里面也没有新增的邮件. 这让本人觉得奇怪,所以就觉得是否被当作垃圾邮件 ...

  6. C++中const简介及用法

    1.const简介 C++中的const关键字的用法非常灵活,而使用const将大大改善程序的健壮性,本人根据各方面查到的资料进行总结如下,期望对朋友们有所帮助. Const 是C++中常用的类型修饰 ...

  7. dedecms导入编辑器

    <?php GetEditor("info","",450,"Diy"); ?>

  8. C++输出中文字符(转)

    C++输出中文字符 1. cout 场景1: 在源文件中定义 const char* str = "中文" 在 VC++ 编译器上,由于Windows环境用 GBK编码,所以字符串 ...

  9. SGU 134.Centroid( 树形dp )

    一道入门树dp, 求一棵树的重心...我是有多无聊去写这种题...傻X题写了也没啥卵用以后还是少写好.. ----------------------------------------------- ...

  10. USACO Section 3.3 Camlot(BFS)

    BFS.先算出棋盘上每个点到各个点knight需要的步数:然后枚举所有点,其中再枚举king是自己到的还是knight带它去的(假如是knight带它的,枚举king周围的2格(网上都这么说,似乎是个 ...