OC数组排序
NSArray *array = @[@"tailong", @"kaersasi", @"airuiliya", @"yingliuzhizhu"];
NSMutableArray *mArray = [NSMutableArray arrayWithObjects:@"genie", @"weizhuang", @"tianming", @"shaoyu", @"gaoyue", nil];
/**
* 1.使用NSSortDescriptor(排序描述符,相当于排序条件)
排序描述符由两个参数组成
key:对于一个给定的集合,对应值的键位对集合中的每个元素进行排序
accending:升序(YES)或降序(NO)
*/
// 给排序的数组生成排序描述符
NSSortDescriptor *arraySortDes = [[NSSortDescriptor alloc] initWithKey:@"self" ascending:YES];
// 不可变数组排序
NSArray *array1 = [array sortedArrayUsingDescriptors:@[arraySortDes]];
//NSLog(@"%@", array1);
for (NSString *obj in array1) {
NSLog(@"%@", obj);
}
// 可变数组排序
[mArray sortUsingDescriptors:@[arraySortDes]];
for (NSString *str in mArray) {
NSLog(@"%@", str);
}
//新建一个Person类
Person.h
#import <Foundation/Foundation.h> @interface Person : NSObject @property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *gender;
@property (nonatomic, assign) NSInteger age; // 自定义初始化方法
- (instancetype)initWithName:(NSString *)name
andGender:(NSString *)gender
andAge:(NSInteger)age; // 遍历构造器
+ (instancetype)personWithName:(NSString *)name
andGender:(NSString *)gender
andAge:(NSInteger)age; // 比较两个人的姓名
- (NSComparisonResult)compareByName:(Person *)person; // 比较两个人的年龄
- (NSComparisonResult)compareByAge:(Person *)person; @end
Person.m
#import "Person.h" @implementation Person // 自定义初始化方法
- (instancetype)initWithName:(NSString *)name
andGender:(NSString *)gender
andAge:(NSInteger)age {
if (self = [super init]) {
self.name = name;
self.gender = gender;
self.age = age;
}
return self;
} // 遍历构造器
+ (instancetype)personWithName:(NSString *)name
andGender:(NSString *)gender
andAge:(NSInteger)age {
Person *p = [[Person alloc] initWithName:name andGender:gender andAge:age];
return p;
} // 重写description
- (NSString *)description {
return [NSString stringWithFormat:@"name = %@, gender = %@, age = %ld", self.name, _gender, _age];
} // 比较两个人的姓名(降序在[]前面加个-)
- (NSComparisonResult)compareByName:(Person *)person {
return [self.name compare:person.name];
} // 比较两个人的年龄(降序把>改为<)
- (NSComparisonResult)compareByAge:(Person *)person {
return self.age > person.age;
} @end
// 数组中存放自定义对象进行排序
Person *p1 = [Person personWithName:@"tailong" andGender:@"男" andAge:27];
Person *p2 = [Person personWithName:@"manwang" andGender:@"男" andAge:24];
Person *p3 = [Person personWithName:@"hanbing" andGender:@"女" andAge:20];
Person *p4 = [Person personWithName:@"airuiliya" andGender:@"女" andAge:21];
Person *p5 = [Person personWithName:@"jiansheng" andGender:@"男" andAge:23];
//不可变数组
NSArray *personArray = @[p1, p2, p3, p4, p5];
// 按照姓名进行排序
// 创建排序描述对象
NSSortDescriptor *sortDes1 = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
NSArray *personArray1 = [personArray sortedArrayUsingDescriptors:@[sortDes1]];
NSLog(@"%@", personArray1);
// 按照年龄进行排序
NSSortDescriptor *sortDes2 = [[NSSortDescriptor alloc] initWithKey:@"age" ascending:NO];
NSArray *personArray2 = [personArray sortedArrayUsingDescriptors:@[sortDes2]];
NSLog(@"%@", personArray2);
// 可变数组
NSMutableArray *mPersonArray = [@[p1, p2, p3, p4, p5] mutableCopy];
// 按照年龄进行排序
NSSortDescriptor *mSortDes1 = [[NSSortDescriptor alloc] initWithKey:@"age" ascending:YES];
[mPersonArray sortUsingDescriptors:@[mSortDes1]];
NSLog(@"%@", mPersonArray);
// 按照姓名进行排序
NSSortDescriptor *mSortDes2 = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO];
[mPersonArray sortUsingDescriptors:@[mSortDes2]];
NSLog(@"%@", mPersonArray);
/**
* 2.使用数组中两个元素比较的方法名排序
*/
// 不可变数组
SEL sel = @selector(compare:);
array = [array sortedArrayUsingSelector:sel];
NSLog(@"%@", array);
// 可变数组
[mArray sortUsingSelector:@selector(compare:)];
NSLog(@"%@", mArray);
// 数组中存放自定义对象进行排序
// 不可变数组排序
// 按照姓名排序
personArray = [personArray sortedArrayUsingSelector:@selector(compareByName:)];
NSLog(@"%@", personArray);
// 按照年龄进行排序
personArray = [personArray sortedArrayUsingSelector:@selector(compareByAge:)];
NSLog(@"%@", personArray);
// 可变数组
// 按照姓名排序
[mPersonArray sortUsingSelector:@selector(compareByName:)];
NSLog(@"%@", mPersonArray);
// 按照年龄进行排序
[mPersonArray sortUsingSelector:@selector(compareByAge:)];
NSLog(@"%@", mPersonArray);
OC数组排序的更多相关文章
- 数组NSArray与NSMutableArray的常用方法
数组中可以放任何类型的数据,并且一个数组中的元素类型可以不一致.只要是(id类型)对象. NSArray 1.初始化 NSArray *array = @[]; 2.初始化,最后需要以nil结尾 NS ...
- iOS学习16之OC集合遍历和数组排序
1.集合遍历 1> 遍历 集合(Collection):OC中提供的容器类:数组,字典,集合. 遍历:对集合中元素依次取出的过称叫做遍历. 三种方式:① for循环遍历: ② NSEnumera ...
- OC:Block语法、Block使用、Block实现数组排序
Block //定义一个求两个数最大值函数 int maxValue (int ,int); //函数的实现 int maxValue (int a, int b){ return a > b ...
- OC中数组排序总结
过完节回来,感觉很多东西都生疏了.总结一下数组的排序.应该不会太完美,后续添加补充. OC中的数组排序方法其实不太多,要根据不同的使用场景来使用不同的方法.Foundation框架中一般用到一下几个方 ...
- OC中数组排序的3种方法
总结OC中数组排序3种方法:sortedArrayUsingSelector:;sortedArrayUsingComparator:;sortedArrayUsingDescriptors: 大体上 ...
- OC中用NSSortDescriptor对象进行数组排序
//创建一个数组 NSArray *array = @[@"one", @"two", @"three", @"four" ...
- OC:数组排序、时间格式化字符串
数组排序 //不可变数组的排序 NSArray * arr = [NSArray arrayWithObjects:@"hellow", @"lanou", @ ...
- OC NSArray数组排序
一.一般排序 // 排序 NSArray *arr = @["]; NSArray *newarr = [arr sortedArrayUsingSelector:@selector(com ...
- Objective C中数组排序几种情况的总结
总结OC中数组排序3种方法:sortedArrayUsingSelector:;sortedArrayUsingComparator:;sortedArrayUsingDescriptors: 数组排 ...
随机推荐
- Linux下U盘变成只读
今天用Ubuntu给同学拷贝数据的时候,突然其中一个文件夹U盘就不能复制和删除了.再windows7下可以删除除修改的那个文件夹之外的数据,但修改的那个文件夹死活删除不掉,只读属性也去不掉.再Ubun ...
- Caching查看窗口
闲来无事,做了一个简约的Caching查看窗口,可以方便的查看本地缓存的使用情况: 下面的URL和VersionNum用来查看某个特定资源的特定版本是否存在,分别输入所需信息,点击“检测”,即可在下面 ...
- 【解决方案】VS2013外部工具中添加ildasm.exe
VS2013安装在Win8.1的操作系统中,开始屏幕中找不到ildasm.exe没有显示,于是下面提供了一种方法将ildasm.exe工具添加到VS2013外部工具中,并将反编译的代码输出到VS201 ...
- 潮流设计:15个创意的 3D 字体版式作品欣赏
3D字体设计是真的很棒,它最适用于广告.使用3D文字和不同的惊人效果,例如灯光或纹理带来了很多东西.在版式设计中,最重要的是消息.如果它抓住了用户的注意力,设计工作是在正确的轨道上. 您可能感兴趣的相 ...
- Android 学习笔记之Volley(六)实现获取服务器的字符串响应...
学习内容: 1.使用StringRequest实现获取服务器的字符串响应... 前几篇一直都在对服务——响应过程的源码进行分析,解析了整个过程,那么Volley中到底实现了哪些请求才是我们在开发中 ...
- EF封装类,供参考!
以下是我对EF DB FIRST 生成的ObjectContext类进行封装,代码如下,供参考学习: using System; using System.Collections.Generic; u ...
- mysql修改definer方法
-- 函数.存储过程 select definer from mysql.proc; update mysql.proc set definer='billing@%'; -- 定时事件 sele ...
- 支付宝支付集成,上传RSA公钥一直显示格式错误
碰到同样的问题,支付宝的问题,已有解决方案:https://openhome.alipay.com/platform/keyManage.htm?keyType=partner
- 在GridView列表中使用图片显示记录是否包含附件
在我的前面很多文章中,都介绍过通用附件模块的管理,本篇随笔主要介绍在一些应用模块中的列表展示中,包含附件的记录,在GridView列表界面中使用图标来快速显示是否有附件的情况. 1.通用附件模块的应用 ...
- 【C#】属性(Attribute)
如果程序员是猫,你是哪只猫? 这个是我一直都很喜欢的一个技术,不是很麻烦,也不是很难理解,和反射配合起来,只有你想不到没有做不到的用途(夸张了哈). 运用范围 程序集,模块,类型(类,结构,枚举,接口 ...