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开发.一起学习,共同进步.假设您是零基 ...
随机推荐
- Minor【 PHP框架】5.事件
框架Github地址:github.com/Orlion/Minor (如果觉得还不错给个star哦(^-^)V) 框架作者: Orlion 知乎:https://www.zhihu.com/peop ...
- Android开发输入法遮盖屏幕底部按钮
方法一: 在你的activity中的oncreate中setContentView之前写上这个代码: getWindow().setSoftInputMode(WindowManager.Layout ...
- 关于javascript中限定时间内防止按钮重复点击的思路
前面的话 有一天心血来潮,1分钟内重复点击了多次博客园首页的刷新博文列表的刷新按钮.果不其然,ip当时就被禁用了.后来,重启自己的路由器,重新获取ip才可以访问博客园主页.那么,设置一个限定时间内(比 ...
- Android音视频之MediaPlayer音视频播放
前言: 昨天总结了视频录制,今天来学习一下视频的播放,Android的视频播放主要采用MediaPlayer类. MediaPlayer介绍 MediaPlayer类可用于控制音频/视频文件或流的播放 ...
- 窥探Swift之别样的枚举类型
想必写过程序的童鞋对枚举类型并不陌生吧,使用枚举类型的好处是多多的,在这儿就不做过多的赘述了.Fundation框架和UIKit中的枚举更是数不胜数,枚举可以使你的代码更易阅读并且可以提高可维护性.在 ...
- ES6学习--搭建环境
ECMAScript 6(以下简称ES6)是JavaScript语言的下一代标准,已经在2015年6月正式发布了.它的目标,是使得JavaScript语言可以用来编写复杂的大型应用程序,成为企业级开发 ...
- Java内存模型深度解析:基础部分--转
原文地址:http://www.codeceo.com/article/java-memory-1.html 并发编程模型的分类 在并发编程中,我们需要处理两个关键问题:线程之间如何通信及线程之间如何 ...
- 真正解决问题:maven eclipse tomcat java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener--转
原文地址:http://www.cnblogs.com/amosli/p/4067665.html 在使用eclipse进行整合springmvc时经常会碰到这样的异常: java.lang.Clas ...
- 1Z0-053 争议题目解析470
1Z0-053 争议题目解析470 考试科目:1Z0-053 题库版本:V13.02 题库中原题为: 470.Which NLS parameter can be used to change the ...
- IEE重建表完全释放磁盘空间具体步骤参考
环境:RHEL 5.3 + IEE 5.1.40 本文目的:指导项目侧人员再遇到此类改动需求时可以自己更改.需求:mr_intrainterfreq表重建,历史数据全部删掉. 1.停库: 1.1确认现 ...