Objective-C 排序
在Objective-C中,排序分为:
1、Foundation框架中的对象排序
2、自定义对象排序
例子:每个学生都有一个成绩score属性,根据成绩score对学生排序
自定义对象 Student.h

Student.m

main.m
#import <Foundation/Foundation.h>
#import "Student.h" int main(int argc, const char * argv[]) {
@autoreleasepool { //1、Foundation框架中的对象排序
NSArray *arr = @[@, @, @, @, @];
NSLog(@"排序前: %@", arr);
// 注意: 想使用compare方法对数组中的元素进行排序, 那么数组中的元素必须是Foundation框架中的对象, 也就是说不能是自定义对象
NSArray *newArr = [arr sortedArrayUsingSelector:@selector(compare:)];
NSLog(@"排序后: %@", newArr); //2、自定义对象排序 Student *stu1 = [Student new];
stu1.score = ; Student *stu2 = [Student new];
stu2.score = ; Student *stu3 = [Student new];
stu3.score = ; Student *stu4 = [Student new];
stu4.score = ; NSArray *studentArr = @[stu1, stu2, stu3, stu4];
NSLog(@"排序前: %@", studentArr); // 按照学生的成绩进行排序
// 不能使用compare:方法对自定义对象进行排序
// NSArray *newArr = [arr sortedArrayUsingSelector:@selector(compare:)]; // 该方法默认会按照升序排序
NSArray *newStudentArr = [studentArr sortedArrayWithOptions:NSSortStable usingComparator:^NSComparisonResult(Student *obj1, Student *obj2) {
//升序
return obj1.score > obj2.score;
//降序
// return obj1.score < obj2.score;
}];
NSLog(@"成绩排序后: %@", newStudentArr);
return ;
}
return ;
}
结果:

3、自定义对象多个元素排序
JKStudent.h里面:
#import <Foundation/Foundation.h> @interface JKStudent : NSObject @property (nonatomic,assign) int age;
@property (nonatomic,retain) NSString *name; -(id)initWithAge:(int)age andName:(NSString*)name; + (JKStudent *) studentWithAge:(int)age andName:(NSString *)name; //排序规则
//比较年龄
-(NSComparisonResult)compare:(JKStudent*)otherStudent;
//比较姓名
-(NSComparisonResult)compareName:(JKStudent *)otherStudent;
//先年龄后姓名
-(NSComparisonResult)compareAgeAndName:(JKStudent *)otherStudent; @end
JKStudent.m里面:
#import "JKStudent.h"
@implementation JKStudent
-(id)initWithAge:(int)age andName:(NSString*)name{
self = [super init];
if (self) {
self.age = age;
self.name = name;
}
return self;
}
+ (JKStudent *) studentWithAge:(int)age andName:(NSString *)name
{
return [[JKStudent alloc]initWithAge:age andName:name];
}
-(NSString *)description{
return [NSString stringWithFormat:@"age:%d name:%@",self.age,self.name];
}
//排序规则
//比较年龄
-(NSComparisonResult)compare:(JKStudent*)otherStudent{
if(self.age>otherStudent.age){
return NSOrderedDescending;
}else if (self.age == otherStudent.age){
return NSOrderedSame;
}else{
return NSOrderedAscending;
}
}
//比较姓名
-(NSComparisonResult)compareName:(JKStudent *)otherStudent{
return [self.name compare:otherStudent.name];
}
//先年龄后姓名
-(NSComparisonResult)compareAgeAndName:(JKStudent *)otherStudent{
//先比较年龄
if(self.age>otherStudent.age){
return NSOrderedDescending;
}else if (self.age == otherStudent.age){
//比较姓名
return [self.name compare:otherStudent.name];
}else{
return NSOrderedAscending;
}
}
//创建5个学生
JKStudent *student1 = [JKStudent studentWithAge: andName:@"JACK"];
JKStudent *student2 = [JKStudent studentWithAge: andName:@"LUSON"];
JKStudent *student3 = [JKStudent studentWithAge: andName:@"EASON"];
JKStudent *student4 = [JKStudent studentWithAge: andName:@"LINA"];
JKStudent *student5 = [JKStudent studentWithAge: andName:@"KATU"]; //初始数组
NSArray *studentArray1 = [NSArray arrayWithObjects:student1,student2,student3,student4,student5, nil];
NSLog(@"初始数组:%@",studentArray1); //按照年龄排序
NSArray *studentArray2 = [studentArray1 sortedArrayUsingSelector:@selector(compare:)];
NSLog(@"按照年龄排序:%@",studentArray2); //按照名字排序
NSArray *studentArray3 = [studentArray1 sortedArrayUsingSelector:@selector(compareName:)];
NSLog(@"按照名字排序:%@",studentArray3); //按照先年龄再名字排序
NSArray *studentArray4 = [studentArray1 sortedArrayUsingSelector:@selector(compareAgeAndName:)];
NSLog(@"按照先年龄再名字排序:%@",studentArray4);

Objective-C 排序的更多相关文章
- lightgbm用于排序
一. LTR(learning to rank)经常用于搜索排序中,开源工具中比较有名的是微软的ranklib,但是这个好像是单机版的,也有好长时间没有更新了.所以打算想利用lightgbm进行排序, ...
- Automake
Automake是用来根据Makefile.am生成Makefile.in的工具 标准Makefile目标 'make all' Build programs, libraries, document ...
- Objective—C中的排序及Compare陷阱
campare陷阱 NSString有多个compare相关方法: - (NSComparisonResult)compare:(NSString *)string; - (NSComparisonR ...
- 利用 NSSortDescriptor 对 NSMutableArray 排序
有时我们在NSMutableArray中存的是网络请求返回的数据,而每一个元素又是一个NSDictionary,如果这时候需要把数组中的元素按照每个元素字典中某一个key来排序,那么我们可以利用Obj ...
- 拓扑排序的 +Leapms 线性规划模型
知识点 拓扑排序 拓扑排序的+Leapms模型 无圈有向图 一个图G(V,E), 如果边有向且不存在回路,则为无圈有向图.在无圈有向图上可以定义拓扑排序.下图是一个无圈有向图的例子. 拓扑排序 给定一 ...
- A Diversity-Promoting Objective Function for Neural Conversation Models论文阅读
本文来自李纪为博士的论文 A Diversity-Promoting Objective Function for Neural Conversation Models 1,概述 对于seq2seq模 ...
- NSArray进行汉字排序
由于NSArray并不直接支持对汉字的排序,这就要通过将汉字转换成拼音完毕按A~Z的排序,这看起来是个头疼的问题.由于牵扯到汉字转为拼音,kmyhy给出一个较易实现的方法,获取汉字的首字的首字母,如将 ...
- 浅谈iOS开发中多语言的字符串排序
一.前言 在iOS开发中,一个经常的场景是利用tableview展示一组数据,以很多首歌曲为例子.为了便于查找,一般会把这些歌曲按照一定的顺序排列,还会加上索引条以便于快速定位. 由于歌曲名可能有数字 ...
- IOS数组按中文关键字以字母序排序
本文转载至 http://blog.csdn.net/xunyn/article/details/7882087 iosobjective cuser框架通讯 IOS项目中会用到对通讯录的联系人或是会 ...
- iOS开发核心语言Objective C —— 全部知识点总结
本分享是面向有意向从事iOS开发的伙伴及苹果产品的发烧友,亦或是已经从事了iOS的开发人员,想进一步提升者.假设您对iOS开发有极高的兴趣,能够与我一起探讨iOS开发.一起学习,共同进步.假设您是零基 ...
随机推荐
- VS中行号对齐的辅助线(虚线)去除
3张图 2 3
- LINQ系列:LINQ to ADO.NET概述
LINQ to ADO.NET 包括两种独立的技术: LINQ to DataSet 和 LINQ to SQL. 使用 LINQ to DataSet 可以对DataSet 执行丰富而优化的查询,而 ...
- Inno Steup 打包加入文件夹
在[Files]段下,添加下面代码Source: "执行文件.exe"; DestDir: "{app}"; Flags: igNoreversionSourc ...
- Math的三个将小数值舍入为整数方法
舍入规则: Math.ceil():执行向上舍入,即它总是将数值向上舍入为最接近的整数: Math.floor():执行向下舍入,即它总是将数值向下舍入为最接近的整数: Math.round():执行 ...
- 前端学PHP之面向对象系列第二篇——魔术方法
× 目录 [1]构造方法 [2]析构方法 [3]不可访问属性[4]对象复制[5]字符串[6]对象不存在[7]自动加载类[8]串行化[9]函数调用 前面的话 php在面向对象部分有很多相关的魔术方法,这 ...
- 应用程序框架实战二十二 : DDD分层架构之仓储(层超类型基础篇)
前一篇介绍了仓储的基本概念,并谈了我对仓储的一些认识,本文将实现仓储的基本功能. 仓储代表聚合在内存中的集合,所以仓储的接口需要模拟得像一个集合.仓储中有很多操作都是可以通用的,可以把这部分操作抽取到 ...
- SubSonic3.0使用外连接查询时查询不出数据的问题修改
今天在开发时,要使用到外连接查询,如图 老是查不出数据,所以就追踪了一下代码,发现查询后生成的SQL语句变成了内连接了,真是晕 然后继续Debug,发现原来SqlQuery类在调用LeftInnerJ ...
- 关于SubSonic3.0插件使用实体进行更新操作时(执行T.Update()或T.Save()),某些列无法进行修改操作的问题处理
SubSonic3.0插件在创建实体后,对实体进行赋值操作时,为了去除一些不必要更新的字段,减少更新的内容,会将更新内容与默认值进行比较,如果默认值与当前更新的内容相等时,则不提交更新本列,这主要是为 ...
- C# 对象锁
现在有一种场景,服务端将一个任务派发给10个客户端执行,所有客户端执行完后,整个任务才算执行完. 每个客户端执行完后,通过http发送一个完成的消息给服务端,服务端发送邮件通知给用户. 当10个客户端 ...
- 给Easyui combobox设定默认值
今天做到那个北理工二期的项目,里面刚好有几个dialog需要弄一个默认值,一般是选择启用与否,但是,为了方便用户,最好有一个默认值,所以,增加一个默认值的属性.代码入下: JS代码: ...