在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. 由于Replication,DBCC Shrink不能收缩Log File

    使用Backup创建测试环境之后,发现testdb的Log File过大,达到400GB,由于测试环境实际上不需要这么大的Log Space,占用400GB的Disk Space实在浪费Disk Re ...

  2. Security2:Create User

    User 用于访问DB Users based on logins in master (This is the most common type of user.) User based on a ...

  3. es6学习笔记一数组(下)

    entries() 方法: 概述:    entries() 方法返回一个 Array Iterator(数组迭代) 对象,该对象包含数组中每一个索引的键值对. 示例: let arr = [&quo ...

  4. 【原创】开源Math.NET基础数学类库使用(01)综合介绍

                   本博客所有文章分类的总目录:[总目录]本博客博文总目录-实时更新  开源Math.NET基础数学类库使用总目录:[目录]开源Math.NET基础数学类库使用总目录 前言 ...

  5. WebService 学习之路(一):了解并使用webService

    webService主要用于向其他系统提供接口以便调用,系统间可能开发语言等完全不同,根据约定的接口规范,调用者传递相关参数进行接口调用,服务方根据传入的条件进行业务处理并进行结果返回. webSer ...

  6. 结婚虽易,终老不易:EntityFramework和AutoMapper的婚后生活

    写在前面 我到底是什么? 越界的可怕 做好自己 后记 上一篇<恋爱虽易,相处不易:当EntityFramework爱上AutoMapper>文章的最后提到,虽然AutoMapper为了En ...

  7. 数据库操作提示:Specified key was too long; max key length is 767 bytes

    操作重现: 法1:新建连接——>新建数据库——>右键数据库导入脚本——>提示:Specified key was too long; max key length is 767 by ...

  8. chrome扩展程序开发

    首先,明确两个概念的区别:chrome扩展程序和Web Apps.具体参考:http://www.chromi.org/archives/10106 本文只讨论chrome扩展程序. 最好的开发教程莫 ...

  9. 如何用easyui+JAVA 实现动态拼凑datagrid表格(续)

    前面一段时间写了一篇文章: 如何用easyui+JAVA 实现动态拼凑datagrid表格 这篇文章的话,效果是可以实现,但是经过我反复试验,还是存在一些问题的. 今天这篇文章就是向大家介绍下如何避免 ...

  10. Java中的反射机制

    Java反射机制 反射机制定义 反射机制是Java语言中一个非常重要的特性,它允许程序在运行时进行自我检查,同时也允许其对内部成员进行操作.由于反射机制能够实现在运行时对类进行装载,因此能够增加程序的 ...