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

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

1、简单排序(sortedArrayUsingSelector:)

2、利用block语法(sortedArrayUsingComparator:)

3、高级排序(sortedArrayUsingDescriptors:)

1、简单排序(sortedArrayUsingSelector:)

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

 #pragma mark 数组排序 sortedArrayUsingSelector

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

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

 #import <Foundation/Foundation.h>
@class Book;
@interface Student : NSObject
@property(nonatomic,retain)NSString *firstName;
@property(nonatomic,retain)NSString *lastName;
@property(nonatomic,retain)Book *book;
//快速创建student
+ (id)studentWithFirstName:(NSString*)firstName lastName:(NSString*)lastName;
//比较规则
- (NSComparisonResult)compareStudent:(Student*)stu;
@end #import "Student.h"
#import "Book.h"
@implementation Student
+ (id)studentWithFirstName:(NSString*)firstName lastName:(NSString*)lastName
{
//类方法自动释放所以使用autorelease
Student *stu = [[[Student alloc]init]autorelease];
stu.firstName = firstName;
stu.lastName = lastName;
return stu;
} - (NSComparisonResult)compareStudent:(Student*)stu
{
//1.先按照姓排序
NSComparisonResult result = [self.lastName compare:stu.lastName]; if(result == NSOrderedSame)
{
result = [self.firstName compare:stu.firstName];
}
return result;
}
- (NSString *)description
{
return [NSString stringWithFormat:@"[%@ %@]",self.lastName,self.firstName];
} - (void)dealloc
{
[_book release];
[_firstName release];
[_lastName release];
[super dealloc];
}
@end #pragma mark 数组排序 arraySort2
void arraySort2()
{
Student *stu = [Student studentWithFirstName:@"QY" lastName:@"Ma"];
Student *stu1 = [Student studentWithFirstName:@"jf" lastName:@"Jia"];
Student *stu2 = [Student studentWithFirstName:@"sy" lastName:@"Qu"];
NSArray *array = [NSArray arrayWithObjects:stu,stu1,stu2,nil];
//指定排序的方法
NSArray *arrayNew = [array sortedArrayUsingSelector:@selector(compareStudent:)];
NSLog(@"arrayNew : %@",arrayNew); }

2.利用block语法(sortedArrayUsingComparator:)

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

 void arraySort3()
{
Student *stu = [Student studentWithFirstName:@"QY" lastName:@"Ma"];
Student *stu1 = [Student studentWithFirstName:@"jf" lastName:@"Jia"];
Student *stu2 = [Student studentWithFirstName:@"sy" lastName:@"Qu"];
NSArray *array = [NSArray arrayWithObjects:stu,stu1,stu2,nil];
//利用block进行排序
NSArray *arrayNew = [array sortedArrayUsingComparator:^NSComparisonResult(Student *obj1, Student *obj2) {
//1.先按照姓排序
NSComparisonResult result = [obj1.lastName compare:obj2.lastName]; if(result == NSOrderedSame)
{
result = [obj1.firstName compare:obj2.firstName];
}
return result;
}];
NSLog(@"arrayNew : %@",arrayNew);
}

3.高级排序(sortedArrayUsingDescriptors:)

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

代码如下:

 //Book
#import <Foundation/Foundation.h> @interface Book : NSObject
@property(nonatomic,retain)NSString *name;
+ (id)bookWithName:(NSString*)name;
@end #import "Book.h" @implementation Book
+ (id)bookWithName:(NSString*)name
{
Book *book = [[[Book alloc]init]autorelease];
book.name = name;
return book; }
- (void)dealloc
{
[_name release];
[super dealloc];
}
@end //Student
#import <Foundation/Foundation.h>
@class Book;
@interface Student : NSObject
@property(nonatomic,retain)NSString *firstName;
@property(nonatomic,retain)NSString *lastName;
@property(nonatomic,retain)Book *book;
//快速创建student
+ (id)studentWithFirstName:(NSString*)firstName lastName:(NSString*)lastName;
//快速创建student
+ (id)studentWithFirstName:(NSString*)firstName lastName:(NSString*)lastName bookName:(NSString*)bookName;
//比较规则
- (NSComparisonResult)compareStudent:(Student*)stu;
@end #import "Student.h"
#import "Book.h"
@implementation Student
+ (id)studentWithFirstName:(NSString*)firstName lastName:(NSString*)lastName
{
//类方法自动释放所以使用autorelease
Student *stu = [[[Student alloc]init]autorelease];
stu.firstName = firstName;
stu.lastName = lastName;
return stu;
} //快速创建student
+ (id)studentWithFirstName:(NSString*)firstName lastName:(NSString*)lastName bookName:(NSString*)bookName
{
Student *stu = [Student studentWithFirstName:firstName lastName:lastName];
stu.book = [Book bookWithName:bookName];
return stu; }
- (NSComparisonResult)compareStudent:(Student*)stu
{
//1.先按照姓排序
NSComparisonResult result = [self.lastName compare:stu.lastName]; if(result == NSOrderedSame)
{
result = [self.firstName compare:stu.firstName];
}
return result;
}
- (NSString *)description
{
return [NSString stringWithFormat:@"[%@ %@ %@]",self.lastName,self.firstName,self.book.name];
} - (void)dealloc
{
[_book release];
[_firstName release];
[_lastName release];
[super dealloc];
}
@end //将排序描述按照顺序存入到数组中
#pragma mark 数组排序 sortedArrayUsingDescriptors 排序描述器
void arraySort5()
{
Student *stu1 = [Student studentWithFirstName:@"QY" lastName:@"Ma" bookName:@"book3"];
Student *stu2 = [Student studentWithFirstName:@"JF" lastName:@"Jia" bookName:@"book1"];
Student *stu3 = [Student studentWithFirstName:@"SY" lastName:@"Qu" bookName:@"book4"];
NSArray *array = [NSArray arrayWithObjects:stu1,stu2,stu3, nil];
//放入到描述器里面
NSSortDescriptor *bookNameDesc = [NSSortDescriptor sortDescriptorWithKey:@"book.name" ascending:YES];
NSSortDescriptor *lastNameDesc = [NSSortDescriptor sortDescriptorWithKey:@"lastName" ascending:YES];
NSSortDescriptor *firstName = [NSSortDescriptor sortDescriptorWithKey:@"firstName" ascending:YES];
NSArray *array1 = [NSArray arrayWithObjects:bookNameDesc,lastNameDesc,firstName,nil];
NSArray *arrayNew = [array sortedArrayUsingDescriptors:array1];
NSLog(@"arrayNew :%@",arrayNew); }

16.Object-C--NSArray数组的排序的更多相关文章

  1. Objective-C之NSArray(数组)默认排序与自定义排序

    在讲OC中数组的排序之前我先上一段代码,它是简单数组排序的一种方法(也就是元素是字符串或者数据的数组,因为后面要讲元素为类的数组排序) 代码1: NSArray *sortArr4 = [sortAr ...

  2. 给object数组进行排序(排序条件是每个元素对象的属性个数)

    从汤姆大叔的博客里看到了6个基础题目:本篇是第3题 - 给object数组进行排序(排序条件是每个元素对象的属性个数) 解题关键: 1.Array.sort的用法 2.object的属性数量的统计 解 ...

  3. [JS深入学习]——数组对象排序

    (转) JavaScript实现多维数组.对象数组排序,其实用的就是原生的sort()方法,用于对数组的元素进行排序. sort() 方法用于对数组的元素进行排序.语法如下: arrayObject. ...

  4. 利用Comparable接口实现对对象数组的排序

    Arrays 类中的sort方法承诺可以对对象数组进行排序,但是需要对象所属的类实现Comparable接口 任何实现Comparable接口的对象都需要实现该方法 并且在Java SE 5.0之前该 ...

  5. iOS之数组的排序(升序、降序及乱序)

    #pragma mark -- 数组排序方法(升序) - (void)arraySortASC{ //数组排序 //定义一个数字数组 NSArray *array = @[@(3),@(4),@(2) ...

  6. javascript:算法之数组sort排序

    数组sort排序 sort比较次数,sort用法,sort常用 描述 方法sort()将在原数组上对数组元素进行排序,即排序时不创建新的数组副本.如果调用方法sort()时没有使用参数,将按字母顺序( ...

  7. PHP array_multisort—对多个数组或多维数组进行排序

    PHP中array_multisort可以用来一次对多个数组进行排序,或者根据某一维或多维对多维数组进行排序. 关联(string)键名保持不变,但数字键名会被重新索引. 输入数组被当成一个表的列并以 ...

  8. OC NSArray 数组

    # OC NSArray 数组 NSArray常用方法 获取数组中第一位元素 array.firstObject 获取数组中最后一个元素 array.lastObject 获取数组中指定索引下标的元素 ...

  9. javascript数组对象排序

    javascript数组对象排序 JavaScript数组内置排序函数 javascript内置的sort函数是多种排序算法的集合 JavaScript实现多维数组.对象数组排序,其实用的就是原生的s ...

随机推荐

  1. MariaDB Galera Cluster 部署(如何快速部署 MariaDB 集群)

    MariaDB Galera Cluster 部署(如何快速部署 MariaDB 集群)  OneAPM蓝海讯通7月3日 发布 推荐 4 推荐 收藏 14 收藏,1.1k 浏览 MariaDB 作为 ...

  2. Java 8怎么了:局部套用vs闭包

    [编者按]本文作者为专注于自然语言处理多年的 Pierre-Yves Saumont,Pierre-Yves 著有30多本主讲 Java 软件开发的书籍,自2008开始供职于 Alcatel-Luce ...

  3. POJ 3982 序列(JAVA,简单,大数)

    题目 //在主类中 main 方法必须是 public static void 的,在 main 中调用非static类时会有警告信息, //可以先建立对象,然后通过对象调用方法: import ja ...

  4. 安装微软ASP.NET MVC 4,运行以下的包管理器控制台命令

    (菜鸟,勿喷,有错求指正)Asp.net  新建的类库中安装MVC4  .下面是步骤,1+2:打开程序包管理控制台,3:运行Install-Package Microsoft.AspNet.Mvc - ...

  5. 关于android的分辨率

    关于Android的分辨率支持,为大家翻译官方文档 看世界杯的空闲时间,翻译一下官方文档.分辨率问题是大家都很关心的(720×480会不会悲剧),而关于这个问题,android官方的文档无疑最有说服力 ...

  6. fuel openstack 在 VirtualBox 上的部署

    搞了两天,终于搞好了.说说过程: 一开始用的6.1版本的Fuel.iso,按照国内外的教程装了几遍,但是网路验证始终不能通过.自己又不是很懂网络.后来在网上看到说6.1版的需要fuel-master连 ...

  7. 小奇模拟赛9.13 by hzwer

    2015年9月13日NOIP模拟赛 by hzwer    (这是小奇=> 小奇挖矿(explo) [题目背景] 小奇要开采一些矿物,它驾驶着一台带有钻头(初始能力值w)的飞船,按既定路线依次飞 ...

  8. E时代主机,其实做一个小虚拟主机还是不错的

    http://www.idcen.com/ 做微信没有网上测试地址,找了一下发现以前用过的.记录一下.一个100m的虚拟主机需要三四十块钱,做微信,做一个小型网站还是够用的,就是mysql有点问题,不 ...

  9. Linux内核通杀提权漏洞CVE-2016-5195验证

    一.漏洞简介 CVE-2016-5195这个漏洞是linux内核级的本地提权漏洞,原理是linux内核内存子系统在 处理私有只读存储映射的写入时复制机制发现了一个冲突条件.这个漏洞官方给出的影响范围是 ...

  10. java cache过期策略两种实现,一个基于list轮询一个基于timer定时

    最近项目要引入缓存机制,但是不想引入分布式的缓存框架,所以自己就写了一个轻量级的缓存实现,有两个版本,一个是通过timer实现其超时过期处理,另外一个是通过list轮询.       首先要了解下ja ...