热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

16.Object-C--NSArray数组的排序

今天我来总结一下NSArray数组的排序方式。NSArray数组的排序有三种方式:1、简单排序(sortedArrayUsingSelector:)2、利用block语法(sorte

 

今天我来总结一下NSArray数组的排序方式。

NSArray数组的排序有三种方式:

1、简单排序(sortedArrayUsingSelector:)

2、利用block语法(sortedArrayUsingComparator:)

3、高级排序(sortedArrayUsingDescriptors:)

1、简单排序(sortedArrayUsingSelector:)

如果只是对字符串的排序,可以利用sortedArrayUsingSelector:方法就可以了,代码如下:

 1 #pragma mark 数组排序 sortedArrayUsingSelector
 2 
 3 void arraySort1()  4 {  5     NSArray *array = [NSArray arrayWithObjects:@"6",@"9",@"1",@"2",nil];  6     //返回一个排好序的数组,原来的数组不会变 (SEL)中传递的是排序的规则。@selector(compare:)  7     //制定元素的比较方法 compare
 8     NSArray *arrayNew = [array sortedArrayUsingSelector:@selector(compare:)];  9     NSLog(@"arrayNew :%@",arrayNew); 10 }

这里是制定使用Compare方法进行排序。我们也可以制定自己的排序方法

 1 #import 
 2 @class Book;  3 @interface Student : NSObject  4 @property(nonatomic,retain)NSString *firstName;  5 @property(nonatomic,retain)NSString *lastName;  6 @property(nonatomic,retain)Book *book;  7 //快速创建student
 8 + (id)studentWithFirstName:(NSString*)firstName lastName:(NSString*)lastName;  9 //比较规则
10 - (NSComparisonResult)compareStudent:(Student*)stu; 11 @end
12 
13 #import "Student.h"
14 #import "Book.h"
15 @implementation Student 16 + (id)studentWithFirstName:(NSString*)firstName lastName:(NSString*)lastName 17 { 18     //类方法自动释放所以使用autorelease
19     Student *stu = [[[Student alloc]init]autorelease]; 20     stu.firstName = firstName; 21     stu.lastName = lastName; 22     return stu; 23 } 24 
25 - (NSComparisonResult)compareStudent:(Student*)stu 26 { 27     //1.先按照姓排序
28     NSComparisonResult result = [self.lastName compare:stu.lastName]; 29     
30     if(result == NSOrderedSame) 31  { 32         result = [self.firstName compare:stu.firstName]; 33  } 34     return result; 35 } 36 - (NSString *)description 37 { 38     return  [NSString stringWithFormat:@"[%@ %@]",self.lastName,self.firstName]; 39 } 40 
41 - (void)dealloc 42 { 43  [_book release]; 44  [_firstName release]; 45  [_lastName release]; 46  [super dealloc]; 47 } 48 @end
49 
50 
51 #pragma mark 数组排序 arraySort2
52 void arraySort2() 53 { 54     Student *stu = [Student studentWithFirstName:@"QY" lastName:@"Ma"]; 55     Student *stu1 = [Student studentWithFirstName:@"jf" lastName:@"Jia"]; 56     Student *stu2 = [Student studentWithFirstName:@"sy" lastName:@"Qu"]; 57     NSArray *array = [NSArray arrayWithObjects:stu,stu1,stu2,nil]; 58     //指定排序的方法
59     NSArray *arrayNew = [array sortedArrayUsingSelector:@selector(compareStudent:)]; 60     NSLog(@"arrayNew : %@",arrayNew); 61 
62 }

2.利用block语法(sortedArrayUsingComparator:)

这是苹果官方提供的block方法,其中数组排序可以用sortedArrayUsingComparator:方法

 1 void arraySort3()  2 {  3     Student *stu = [Student studentWithFirstName:@"QY" lastName:@"Ma"];  4     Student *stu1 = [Student studentWithFirstName:@"jf" lastName:@"Jia"];  5     Student *stu2 = [Student studentWithFirstName:@"sy" lastName:@"Qu"];  6     NSArray *array = [NSArray arrayWithObjects:stu,stu1,stu2,nil];  7     //利用block进行排序
 8     NSArray *arrayNew =   [array sortedArrayUsingComparator:^NSComparisonResult(Student *obj1, Student *obj2) {  9         //1.先按照姓排序
10         NSComparisonResult result = [obj1.lastName compare:obj2.lastName]; 11         
12         if(result == NSOrderedSame) 13  { 14             result = [obj1.firstName compare:obj2.firstName]; 15  } 16         return result; 17  }]; 18     NSLog(@"arrayNew : %@",arrayNew); 19 }

3.高级排序(sortedArrayUsingDescriptors:)

如果是这样一种情况呢?Student类里有另外一个类的变量,比如说Student类除了lastName,firstName变量,还有一本书,Book类里有个name属性。对Student对象进行排序,有这样的要求:按照Book的name排序,如果是同一本书,那么再按照lastName进行排序,如果lastName也相同,最后按照firstName进行排序。 

代码如下:

 1 //Book
 2 #import 
 3 
 4 @interface Book : NSObject  5 @property(nonatomic,retain)NSString *name;  6 + (id)bookWithName:(NSString*)name;  7 @end
 8 
 9 #import "Book.h"
 10 
 11 @implementation Book  12 + (id)bookWithName:(NSString*)name  13 {  14     Book *book = [[[Book alloc]init]autorelease];  15     book.name = name;  16     return book;  17     
 18 
 19 }  20 - (void)dealloc  21 {  22  [_name release];  23  [super dealloc];  24 }  25 @end
 26 
 27 //Student
 28 #import 
 29 @class Book;  30 @interface Student : NSObject  31 @property(nonatomic,retain)NSString *firstName;  32 @property(nonatomic,retain)NSString *lastName;  33 @property(nonatomic,retain)Book *book;  34 //快速创建student
 35 + (id)studentWithFirstName:(NSString*)firstName lastName:(NSString*)lastName;  36 //快速创建student
 37 + (id)studentWithFirstName:(NSString*)firstName lastName:(NSString*)lastName bookName:(NSString*)bookName;  38 //比较规则
 39 - (NSComparisonResult)compareStudent:(Student*)stu;  40 @end
 41 
 42 #import "Student.h"
 43 #import "Book.h"
 44 @implementation Student  45 + (id)studentWithFirstName:(NSString*)firstName lastName:(NSString*)lastName  46 {  47     //类方法自动释放所以使用autorelease
 48     Student *stu = [[[Student alloc]init]autorelease];  49     stu.firstName = firstName;  50     stu.lastName = lastName;  51     return stu;  52 }  53 
 54 
 55 //快速创建student
 56 + (id)studentWithFirstName:(NSString*)firstName lastName:(NSString*)lastName bookName:(NSString*)bookName  57 {  58     Student *stu = [Student studentWithFirstName:firstName lastName:lastName];  59     stu.book = [Book bookWithName:bookName];  60     return stu;  61 
 62 }  63 - (NSComparisonResult)compareStudent:(Student*)stu  64 {  65     //1.先按照姓排序
 66     NSComparisonResult result = [self.lastName compare:stu.lastName];  67     
 68     if(result == NSOrderedSame)  69  {  70         result = [self.firstName compare:stu.firstName];  71  }  72     return result;  73 }  74 - (NSString *)description  75 {  76     return  [NSString stringWithFormat:@"[%@ %@ %@]",self.lastName,self.firstName,self.book.name];  77 }  78 
 79 - (void)dealloc  80 {  81  [_book release];  82  [_firstName release];  83  [_lastName release];  84  [super dealloc];  85 }  86 @end
 87 
 88 //将排序描述按照顺序存入到数组中
 89 #pragma mark 数组排序 sortedArrayUsingDescriptors 排序描述器
 90 void arraySort5()  91 {  92     Student *stu1 = [Student studentWithFirstName:@"QY" lastName:@"Ma" bookName:@"book3"];  93     Student *stu2 = [Student studentWithFirstName:@"JF" lastName:@"Jia" bookName:@"book1"];  94     Student *stu3 = [Student studentWithFirstName:@"SY" lastName:@"Qu" bookName:@"book4"];  95     NSArray *array = [NSArray arrayWithObjects:stu1,stu2,stu3, nil];  96     //放入到描述器里面
 97     NSSortDescriptor *bookNameDesc = [NSSortDescriptor sortDescriptorWithKey:@"book.name" ascending:YES];  98     NSSortDescriptor *lastNameDesc = [NSSortDescriptor sortDescriptorWithKey:@"lastName" ascending:YES];  99     NSSortDescriptor *firstName = [NSSortDescriptor sortDescriptorWithKey:@"firstName" ascending:YES]; 100     NSArray *array1 = [NSArray arrayWithObjects:bookNameDesc,lastNameDesc,firstName,nil]; 101     NSArray *arrayNew = [array sortedArrayUsingDescriptors:array1]; 102     NSLog(@"arrayNew :%@",arrayNew); 103 
104 }

 


推荐阅读
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • 如何使用Java获取服务器硬件信息和磁盘负载率
    本文介绍了使用Java编程语言获取服务器硬件信息和磁盘负载率的方法。首先在远程服务器上搭建一个支持服务端语言的HTTP服务,并获取服务器的磁盘信息,并将结果输出。然后在本地使用JS编写一个AJAX脚本,远程请求服务端的程序,得到结果并展示给用户。其中还介绍了如何提取硬盘序列号的方法。 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 本文介绍了一个在线急等问题解决方法,即如何统计数据库中某个字段下的所有数据,并将结果显示在文本框里。作者提到了自己是一个菜鸟,希望能够得到帮助。作者使用的是ACCESS数据库,并且给出了一个例子,希望得到的结果是560。作者还提到自己已经尝试了使用"select sum(字段2) from 表名"的语句,得到的结果是650,但不知道如何得到560。希望能够得到解决方案。 ... [详细]
  • Android JSON基础,音视频开发进阶指南目录
    Array里面的对象数据是有序的,json字符串最外层是方括号的,方括号:[]解析jsonArray代码try{json字符串最外层是 ... [详细]
  • 本文详细介绍了Spring的JdbcTemplate的使用方法,包括执行存储过程、存储函数的call()方法,执行任何SQL语句的execute()方法,单个更新和批量更新的update()和batchUpdate()方法,以及单查和列表查询的query()和queryForXXX()方法。提供了经过测试的API供使用。 ... [详细]
  • 本文详细介绍了Java中vector的使用方法和相关知识,包括vector类的功能、构造方法和使用注意事项。通过使用vector类,可以方便地实现动态数组的功能,并且可以随意插入不同类型的对象,进行查找、插入和删除操作。这篇文章对于需要频繁进行查找、插入和删除操作的情况下,使用vector类是一个很好的选择。 ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
  • 本文详细介绍了在ASP.NET中获取插入记录的ID的几种方法,包括使用SCOPE_IDENTITY()和IDENT_CURRENT()函数,以及通过ExecuteReader方法执行SQL语句获取ID的步骤。同时,还提供了使用这些方法的示例代码和注意事项。对于需要获取表中最后一个插入操作所产生的ID或马上使用刚插入的新记录ID的开发者来说,本文提供了一些有用的技巧和建议。 ... [详细]
  • 前景:当UI一个查询条件为多项选择,或录入多个条件的时候,比如查询所有名称里面包含以下动态条件,需要模糊查询里面每一项时比如是这样一个数组条件:newstring[]{兴业银行, ... [详细]
author-avatar
txwd2008
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有