在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 排序的更多相关文章

  1. lightgbm用于排序

    一. LTR(learning to rank)经常用于搜索排序中,开源工具中比较有名的是微软的ranklib,但是这个好像是单机版的,也有好长时间没有更新了.所以打算想利用lightgbm进行排序, ...

  2. Automake

    Automake是用来根据Makefile.am生成Makefile.in的工具 标准Makefile目标 'make all' Build programs, libraries, document ...

  3. Objective—C中的排序及Compare陷阱

    campare陷阱 NSString有多个compare相关方法: - (NSComparisonResult)compare:(NSString *)string; - (NSComparisonR ...

  4. 利用 NSSortDescriptor 对 NSMutableArray 排序

    有时我们在NSMutableArray中存的是网络请求返回的数据,而每一个元素又是一个NSDictionary,如果这时候需要把数组中的元素按照每个元素字典中某一个key来排序,那么我们可以利用Obj ...

  5. 拓扑排序的 +Leapms 线性规划模型

    知识点 拓扑排序 拓扑排序的+Leapms模型 无圈有向图 一个图G(V,E), 如果边有向且不存在回路,则为无圈有向图.在无圈有向图上可以定义拓扑排序.下图是一个无圈有向图的例子. 拓扑排序 给定一 ...

  6. A Diversity-Promoting Objective Function for Neural Conversation Models论文阅读

    本文来自李纪为博士的论文 A Diversity-Promoting Objective Function for Neural Conversation Models 1,概述 对于seq2seq模 ...

  7. NSArray进行汉字排序

    由于NSArray并不直接支持对汉字的排序,这就要通过将汉字转换成拼音完毕按A~Z的排序,这看起来是个头疼的问题.由于牵扯到汉字转为拼音,kmyhy给出一个较易实现的方法,获取汉字的首字的首字母,如将 ...

  8. 浅谈iOS开发中多语言的字符串排序

    一.前言 在iOS开发中,一个经常的场景是利用tableview展示一组数据,以很多首歌曲为例子.为了便于查找,一般会把这些歌曲按照一定的顺序排列,还会加上索引条以便于快速定位. 由于歌曲名可能有数字 ...

  9. IOS数组按中文关键字以字母序排序

    本文转载至 http://blog.csdn.net/xunyn/article/details/7882087 iosobjective cuser框架通讯 IOS项目中会用到对通讯录的联系人或是会 ...

  10. iOS开发核心语言Objective C —— 全部知识点总结

    本分享是面向有意向从事iOS开发的伙伴及苹果产品的发烧友,亦或是已经从事了iOS的开发人员,想进一步提升者.假设您对iOS开发有极高的兴趣,能够与我一起探讨iOS开发.一起学习,共同进步.假设您是零基 ...

随机推荐

  1. 收集的React.JS资料

    主页 http://facebook.github.io/react/ https://github.com/facebook/react   中文站 http://www.reactjs.cn/ h ...

  2. 学用MVC4做网站六后台管理:6.1.3管理员修改密码

    6.1.3修改密码 需要两个action.一个是点击修改密码的链接要显示修改密码的分部视图(对话框形式):另一个是提交的处理action. 1.打开[AdministratorController]添 ...

  3. java后台搭建学习计划

    1. 使用maven管理java项目 2.linux安装mysql 3.linux安装redis 4. mybatis使用demo 5. cannal使用demo 6. 用spring4开发rest应 ...

  4. 使用RequireJs和Bootstrap模态框实现表单提交

    下面我将使用requirejs结合模态框实现三五行代码部署表单提交操作. 传统开发思路如下:

  5. 关于skip_name_resolve参数的总结

    作为MySQL调优的一部分,很多人都推荐开启skip_name_resolve.这个参数是禁止域名解析的(当然,也包括主机名).很多童鞋会好奇,这背后的原理是什么,什么情况下开启这个参数比较合适. 基 ...

  6. 微软消息分析器(Microsoft Message Analyzer )更新至1.2版-2015-1-20

    就在刚才,收到了微软Connect的邮件推送,大名鼎鼎的微软消息分析器更新至1.2版,并且有公众下载链接,大家可以在这里进行下载. 这里简单摘录一下博客里面提到的新版所增加的功能与功能的改进方面. G ...

  7. vmware网卡设置详解

    转载请注明出处!本文连接及作者.不得用于商业用途! http://hi.baidu.com/quantumcloud/blog/item/9156a6c584996c179c163d5b.html B ...

  8. Linux添加用户(user)到用户组(group)

    将一个用户添加到用户组中,千万不能直接用: usermod -G groupA 这样做会使你离开其他用户组,仅仅做为 这个用户组 groupA 的成员. 应该用 加上 -a 选项: usermod - ...

  9. entity framework 5 批量增删改效率优化

    对于数据的批量增删改最慢的就是操作一条就提交一次事务. 以下是对增删改操作的优化测试 同样的300条数据 批量新增只提交一次事务 用时:10673.5444ms 批量新增只提交一次事务并把contex ...

  10. python查找指定目录下所有文件,以及改文件名的方法

    一: os.listdir(path) 把path目录下的所有文件保存在列表中: >>> import os>>> import re>>> pa ...