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开发.一起学习,共同进步.假设您是零基 ...
随机推荐
- jQuery 2.0.3 源码分析 数据缓存
历史背景: jQuery从1.2.3版本引入数据缓存系统,主要的原因就是早期的事件系统 Dean Edwards 的 ddEvent.js代码 带来的问题: 没有一个系统的缓存机制,它把事件的回调都放 ...
- [汇编与C语言关系]1.函数调用
对于以下程序: int bar(int c, int d) { int e = c + d; return e; } int foo(int a, int b) { return bar(a, b); ...
- DOM-Text类型、Comment类型、CDATASection类型、DocumentType类型、DocumentFragment类型、Attr类型
Text类型 文本节点由Text类型表示,包含的是可以照字面解释的纯文本内容.Text节点具有以下特征: nodeType的值为3 nodeName的值为"text" nodeVa ...
- LTP随笔——本地调用ltp之ltp4j
关于ltp本地调用的相关参考请见LTP的Git项目:https://github.com/HIT-SCIR 以下以/home/lion/Desktop路径为例下面教程中出现的具体路径以你实际配置的为准 ...
- 【实时】DevExpress内存监视
前言 在做项目的时候,我们有时候需要检测项目的内存占用情况,有时候是检测内存泄露~,有时候是查看某段代码执行前后的内存对比,以方便找出问题并以解决. 内存泄漏也称作“存储渗漏”,用动态存储分配函数动态 ...
- 微软的坑:Url重写竟然会引起IIS内核模式缓存不工作
万万没有想到!当初为了解决使用负载均衡时记录客户端IP地址的问题,在IIS URL Rewrite Module中增加了一条URL重写规则(详见迁入阿里云后遇到的Request.UserHostAdd ...
- 1Z0-053 争议题目解析501
1Z0-053 争议题目解析501 考试科目:1Z0-053 题库版本:V13.02 题库中原题为: 501.Note the output of the following query; SQL&g ...
- Apple的LZF算法解析
有关LZF算法的相关解析文档比较少,但是Apple对LZF的开源,可以让我们对该算法进行一个简单的解析.LZFSE 基于 Lempel-Ziv ,并使用了有限状态熵编码.LZF采用类似lz77和lzs ...
- ASP.NET程序开发范例宝典
在整理资料时发现一些非常有用的资料源码尤其是初学者,大部分是平时用到的知识点,可以参考其实现方法,分享给大家学习,但请不要用于商业用途. 如果对你有用请多多推荐给其他人分享. 点击对应章节标题下载本章 ...
- 由面试引发的思考:B/S与C/S究竟是何物
一.现状说明: 就在这金三银四的求职黄金时期,我有幸作为公司的独立技术面试官,拥有最终决定录用权,在倍受上级领导的充分信任下,我也向上级保证,一定要为公司找到合适的人才,就在我满怀信心的情况下面试了一 ...