今天我来总结一下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. 运行时修改TimerTask的执行周期

    java.util.TimerTask类的执行周期period变量的声明如下: /** * Period in milliseconds for repeating tasks. A positive ...

  2. HDU 4569 Special equations(数学推论)

    题目 //想不出来,看了解题报告 /* 题意:给你一个最高幂为4的一元多项式,让你求出一个x使其结果模p*p为0. 题解:f(x)%(p*p)=0那么一定有f(x)%p=0,f(x)%p=0那么一定有 ...

  3. Android 注入详解

    Android下的注入的效果是类似于Windows下的dll注入,关于Windows下面的注入可以参考这篇文章Windows注入术.而Android一般处理器是arm架构,内核是基于linux,因此进 ...

  4. 15.RDD 创建内幕解析

    第15课:RDD创建内幕 RDD的创建方式 Spark应用程序运行过程中,第一个RDD代表了Spark应用程序输入数据的来源,之后通过Trasformation来对RDD进行各种算子的转换,来实现具体 ...

  5. java+内存分配及变量存储位置的区别[转]

    原文来自:http://blog.csdn.net/rj042/article/details/6871030#comments Java内存分配与管理是Java的核心技术之一,之前我们曾介绍过Jav ...

  6. linux下ssh/scp无密钥登陆方法

    一.双方机器都是root用户登陆方法 A为本地主机(即用于控制其他主机的机器) ;B为远程主机(即被控制的机器Server), 假如ip为192.168.60.110;A和B的系统都是Linux 在A ...

  7. 【poj2891-Strange Way to Express Integers】拓展欧几里得-同余方程组

    http://poj.org/problem?id=2891 题意:与中国剩余定理不同,p%ai=bi,此处的ai(i=1 2 3 ……)是不一定互质的,所以要用到的是同余方程组,在网上看到有人称为拓 ...

  8. Spring REST for DELETE Request Method Not Supoorted

    http://stackoverflow.com/questions/22055251/sending-data-with-angularjs-http-delete-request I have a ...

  9. android学习--radiogroup学习

    这个阶段在学习android的相关基本UI现将相关练习的代码粘贴在此便于后期学习之用(radio控件) 效果图:   main_layout.xml <?xml version="1. ...

  10. Java-数据结构与算法-逢3减1-面向对象

    1.要求:有一群人围成一圈数数,逢3退1人,要求算出最后留下来的人的下标2.用面向对象思想,有三个步骤:(1)有哪些类:找名词--"有一群人围成一圈",所以有类People,Peo ...