今天我来总结一下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. leetcode single number系列

    这个系列一共有三题,第一题是一组数里除了一个数出现一次之外,其他数都是成对出现,求这个数. 第二题是一组数里除了两个数出现一次外,其他数都是成对出现,求这两个数 第三题是一组数里除了一个数出现一次外, ...

  2. HDU 3255 Farming (线段树+扫面线,求体积并)

    题意:在一块地上种蔬菜,每种蔬菜有个价值.对于同一块地蔬菜价值高的一定是最后存活,求最后的蔬菜总值. 思路:将蔬菜的价值看做高度的话,题目就转化成求体积并,这样就容易了. 与HDU 3642 Get ...

  3. 知问前端——概述及jQuery UI

    知问系统,是一个问答系统.主要功能:即会员提出问题,会员回答问题.目前比较热门的此类网站有:知乎http://www.zhihu.com.百度知道http://zhidao.baidu.com等.这里 ...

  4. Linux下ps -ef和ps aux的区别及格式详解

    Linux下显示系统进程的命令ps,最常用的有ps -ef 和ps aux.这两个到底有什么区别呢?两者没太大差别,讨论这个问题,要追溯到Unix系统中的两种风格, System V风格和BSD 风格 ...

  5. easyUI Admin 模板

    http://www.oschina.net/p/Easy-Admin?fromerr=23Tfbale

  6. adt导入已经存在于workspace中的项目

    场景: Eclipse中某android项目被delete,但是并未勾选“delete project contents from disk(cannot be undone)”.删除后,下次再想打开 ...

  7. Tomcat日志问题

    [Tomcat日志设定][tomcat控制台日志][log4j日志] 1 Tomcat 日志概述 Tomcat 日志信息分 为 两 类 : 一是运行中的日志,它主要 记录 运行的一些信息,尤其是一些异 ...

  8. haproxy重启

    ps -aux | grep haproxy.cfg sudo kill -9 pidsudo /data/tools/haproxy/sbin/haproxy -f /data/tools/hapr ...

  9. Android百度地图开发05之公交信息检索 + 路线规划

    在上一篇blog中介绍过POI检索的使用,本篇blog主要介绍公交信息检索和线路规划的内容. 公交信息检索 实际上,公交信息检索与POI检索.在线建议检索非常相似,也是把你需要检索的信息发送给百度地图 ...

  10. Database Corruption ->> Fix Database In Suspect State

    昨天在工作中遇到一个情况,就是Development环境中的某台服务器上的某个数据库进入了Suspect状态.以前看书倒是知道说这个状态,不过实际工作当中从来没有遇到过.那么一些背景情况是这样的. 环 ...