1. //方法1,使用自带的比较器

  2. //compare是数组自带的比较方法

  3. NSArray *array=[NSArray arrayWithObjects:@"3",@"1",@"2", nil];

  4. NSArray *array2= [array sortedArrayUsingSelector:@selector(compare:)];

  5. NSLog(@"%@",array2);

结果是升序排列

  1. //方式二:使用块完成排

  2. NSArray *array = [NSArray arrayWithObjects:@"1bc",@"4b6",@"123",@"789",@"3ef", nil];

  3. NSArray *sortedArray = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {

  4.            

  5. //这里的代码可以参照上面compare:默认的排序方法,也可以把自定义的方法写在这里,给对象排序

  6.    NSComparisonResult result = [obj1 compare:obj2];

  7.    return result;

  8. }];

  9. NSLog(@"排序后:%@",sortedArray);

方法3:自定义排序

  1. #import <Foundation/Foundation.h>

  2. @interface Person : NSObject

  3. @property NSString* name;

  4. @property int age;

  5. -(id)initWithNameAndAge:(NSString*) aName and:(int) aAge;

  6. -(NSComparisonResult)comparePersonByAge:(Person *)person;

  7. -(NSComparisonResult)comparePersonByName:(Person *)person;

  8. @end

  1. #import "Person.h"

  2. @implementation Person

  3. @synthesize name,age;

  4. -(id)initWithNameAndAge:(NSString*) aName and:(int) aAge{

  5.    if (self=[super init]) {

  6.        

  7.        name=aName;

  8.        age=aAge;

  9.        

  10.    }

  11.    return self;

  12. }

  13. //自定义排序方法

  14. -(NSComparisonResult)comparePersonByAge:(Person *)person{

  15.    //默认按年龄排序

  16.    NSComparisonResult result = [[NSNumber numberWithInt:person.age] compare:[NSNumber numberWithInt:self.age]];//注意:基本数据类型要进行数据转换

  17.    //如果年龄一样,就按照名字排序

  18.    //if (result == NSOrderedSame) {

  19.    //    result = [self.name compare:person.name];

  20.    //}

  21.    return result;

  22. }

  23. -(NSComparisonResult)comparePersonByName:(Person *)person{

  24.    //默认按年龄排序

  25.    NSComparisonResult result = [ person.name compare:self.name];//注意:基本数据类型要进行数据转换

  26.    //如果年龄一样,就按照名字排序

  27.    if (result == NSOrderedSame) {

  28.        result = [[NSNumber numberWithInt:person.age] compare:[NSNumber numberWithInt:self.age]];

  29.    }

  30.    return result;

  31. }

  32. - (NSString *)description

  33. {

  34.    return [NSString stringWithFormat:@"%@    %d", name,age];

  35. }

  36. @end

  1. #import <Foundation/Foundation.h>

  2. #import "Person.h"

  3. int main(int argc, const char * argv[]) {

  4.    @autoreleasepool {

  5.        Person *p1 = [[Person alloc]initWithNameAndAge:@" qweasadsasd" and:25];

  6.        Person *p2 = [[Person alloc]initWithNameAndAge:@"\t1234" and:28];

  7.        Person *p3 = [[Person alloc]initWithNameAndAge:@"123" and:2];

  8.        Person *p4 = [[Person alloc]initWithNameAndAge:@"zxc" and:89];

  9.        Person *p5 = [[Person alloc]initWithNameAndAge:@"123" and:8];

  10.        

  11.        NSArray * persons = [NSArray arrayWithObjects:p1,p2,p3,p4,p5,nil];

  12.        NSArray *sortedArray = [persons sortedArrayUsingSelector:@selector(comparePersonByName:)];

  13.        NSLog(@"排序后:%@",sortedArray);

  14.    }

  15.    return 0;

  16. }

方法四:高级排序

NSArray 数组排序的更多相关文章

  1. OC NSArray数组排序

    一.一般排序 // 排序 NSArray *arr = @["]; NSArray *newarr = [arr sortedArrayUsingSelector:@selector(com ...

  2. iOS之NSArray数组排序

    一.数组遍历 除了常用的for和for-in遍历外,系统还提供了三种枚举遍历,对于大量的数据遍历可以使用下列三个方法. - (void)enumerateObjectsUsingBlock:(void ...

  3. OC中用NSSortDescriptor对象进行数组排序

    //创建一个数组 NSArray *array = @[@"one", @"two", @"three", @"four" ...

  4. Objective C中数组排序几种情况的总结

    总结OC中数组排序3种方法:sortedArrayUsingSelector:;sortedArrayUsingComparator:;sortedArrayUsingDescriptors: 数组排 ...

  5. NSSortDescriptor对象进行数组排序

    //创建一个数组 NSArray *array = @[@"zhangsan", @"lisi", @"zhonger", @"z ...

  6. objective-c系列-NSArray

    OC数组NSArray 对比         c数组              和       oc数组对象(指针) 定义         int array[10];              NS ...

  7. NSArray与NSMutableArray 数组与可变数组

    1.NSArray 是一个父类,NSMUtableArray是其子类,他们构成了OC的数组.2.NSArray的创建NSArray * array = [[NSArray alloc]initWith ...

  8. OC NSArray 数组

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

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

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

随机推荐

  1. LAMP开发之环境搭建(2014.12.7在ubuntu下)

    Ubuntu下搭建LAMP环境 前言:学习PHP脚本编程语言之前,必须先搭建并熟悉开发环境,开发环境有很多种,例如LAMP.WAMP.MAMP等.这里我搭建的是LAMP环境,即Linux.Apache ...

  2. Tools for Presention

    ZoomIt v4.5 http://technet.microsoft.com/en-us/sysinternals/bb897434.aspx 微软的教师演示工具 主要有放大,画图,倒计时的功能. ...

  3. 一个订单相关的存储过程(MySQL)

    BEGIN DECLARE currentDate VARCHAR(15) ;/*当前日期,有可能包含时分秒 */ DECLARE maxNo INT DEFAULT 0 ; /* 离现在最近的满足条 ...

  4. Win7任务计划自由预设系统定时自动关机

    大家在使用电脑的时候可能会遇到一些需要无人值守让电脑自行执行任务后定时关机的情形,在Win7系统中,我们可以使用"任务计划"设置功能结合shutdown命令灵活设置任务计划,让Wi ...

  5. iOS应用的真机调试

    必须条件:99美元的帐号,没有这个就不用再往下看了. 首先,登录到http://developer.apple.com/devcenter/ios/index.action,如果已经购买了iPhone ...

  6. Window.Open参数、返回值

    一.window.open()支持环境: JavaScript1.0+/JScript1.0+/Nav2+/IE3+/Opera3+ 二.基本语法: window.open(pageURL,name, ...

  7. java 进制转化

    public static void toBinary(int num){ trans(num,1,1); } public static void toHex(int num){ trans(num ...

  8. startDiscovery() and startLeScan().

    You have to start a scan for Classic Bluetooth devices with startDiscovery() and a scan for Bluetoot ...

  9. 【读书笔记】Redis入门

    1:Redis概览 Remote Dictionary Server 远程字典服务 Redis是基于内存的存储 在一台普通的笔记本上,Redis每秒的读取速度可以达到10万 内存读取数据,断电的时候数 ...

  10. sqlserver 类似oracle的rownum功能: row_number

    select row_number() over(order by t.id ) as num,id,name from (SELECT distinct [列 0] as id ,[列 1] as ...