今天我来总结一下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. 【译】 沙箱中的间谍 - 可行的 JavaScript 高速缓存区攻击

    王龑 - MAY 27, 2015 原文连接 The Spy in the Sandbox – Practical Cache Attacks in Javascript 相关论文可在 https:/ ...

  2. HDU1542 Atlantis(矩形面积并)

    #pragma warning (disable:4996) #include<iostream> #include<cstring> #include<string&g ...

  3. HDU 2671 Can't be easier(数学题,点关于直线对称)

    题目 //数学题//直线 y = k * x + b//直线 ax+by+c=0; 点 (x0,y0); 点到直线距离 d = (ax0+by0+c)/sqrt(a^2+b^2) /********* ...

  4. java cookie

    public static void AddCookie(HttpServletResponse response, String key, String value) { Cookie cookie ...

  5. 2014多校第三场1005 || HDU 4891 The Great Pan(模拟)

    题目链接 题意 : 给你n行字符串,问你有多少种理解方式.有两大类的理解 (1){A|B|C|D|...}代表着理解方式可以是A,可以是B或C或者D. (2)$blah blah$,在$$这两个符号中 ...

  6. HTML CSS——margin与padding的初学

    下文引自HTML CSS——margin和padding的学习,作者fengyv,不过加入了一些个人的看法. 你在学习margin和padding的时候是不是懵了,——什么他娘的内边距,什么他娘的外边 ...

  7. OnClientClick="return confirm('确定要删除吗?')"

    OnClientClick="return confirm('确定要删除吗?')"   -----------------------前台代码 OnClientClick用于执行客 ...

  8. Android安卓开发环境搭建详细教程

    安装目录:步骤1 安装JDK步骤2 安装 Android SDK ----http://www.androiddevtools.cn/ 步骤3 安装Tomcat步骤4 安装Ant步骤5 安装Eclip ...

  9. 国内银行CNAPS CODE 查询 苹果开发者,应用内购,需要填写税务相关信息必须的

    https://e.czbank.com/CORPORBANK/QYUK http://weekend.blog.163.com/blog/static/746895820127961346724/

  10. MCU晶体旁边电容的作用及振荡电路的分析

    绝大多数的MCU爱好者对MCU晶体两边要接一个22pF附近的电容不理解,因为这个电容有些时候是可以不要的.参考很多书籍,讲解的很少,往往提到最多的是起稳定作用,负载电容之类的话,都不是很深入理论的分析 ...