今天我来总结一下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. jQuery对象和javascript对象互换

    jquery变js var obj=$("dom"); 或 var obj=jQuery("dom"); js 变 jquery var $jobj=$(obj ...

  2. 【mysql5.6】下载安装

    每次学新技术最烦的就是安软件了..... 下载的mysql5.6 http://dev.mysql.com/downloads/windows/ 一路默认安装.安装后 1.以管理员身份打开C:\WIN ...

  3. POJ 1401

    #include<iostream>using namespace std;int main(){    int num;    int i;    int sum;    cin> ...

  4. ARM 汇编指令

    ARM汇编程序特点: l         所有运算处理都是发生通用寄存器(一般是R0~R14)的之中.所有存储器空间(如C语言变量的本质就是一个存储器空间上的几个BYTE).的值的处理,都是要传送到通 ...

  5. iftop 使用

    测试中常常发现服务器网卡打满,那么这些流量具体占用情况如何呢? 这个时候我们要使用iftop来看看,首先我们要安装: 一. 安装 首先安装libpcap,下载链接:http://www.tcpdump ...

  6. ls 知识点

    ls -R 将会以目录的形式列出所有文件,-S 以文件的大小列出所有文件,-t 将会按照修改时间来列出文件,-i 会显示文件的inode

  7. 绑定CPU

    处理器的亲和性 软亲和性(affinity) 意味着进程并不会在处理器之间频繁迁移,而 硬亲和性(affinity) 则意味着进程需要在您指定的处理器上运行. 通常 Linux 内核都可以很好地对进程 ...

  8. linux下ping加时间戳实时输出到文件 放后台运行

    放后台运行命令:setsid 实时输出命令:unbuffer 加时间戳:awk '{ print $0"\t" strftime("%D_%H:%M:%S",s ...

  9. Qt之QSpacerItem(控件之间的间距不尽相同,可以借助QSpacerItem来设置,并且还可以对QSpacerItem设置QSizePolicy)

    http://blog.csdn.net/u011012932/article/details/51614868

  10. 关于SecureCRT链接虚拟机和开发板的问题

    SecureCRT链接虚拟机后会出现汉字显示乱码问题,一下是解决方案. 点击options 再点击session options 会出现 选择: 主要改两个地方: normal  和 改完之后就可以顺 ...