首页 > 代码库 > NSArray排序方法讲解

NSArray排序方法讲解

NSArray排序方法讲解

给数组排序有着多种方式

最麻烦的是sortedArrayUsingSelector:,其次是sortedArrayUsingDescriptors:,最容易使用的就是sortedArrayUsingComparator:

从最容易使用的开始吧:

    // 原始数组    NSArray *array = @[@"b", @"a", @"x", @"o", @"g", @"o"];        // 排序数组    NSArray *sort = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {        NSString *str1 = obj1;        NSString *str2 = obj2;        return [str1 compare:str2];    }];        // 打印排序数组    [sort enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {        NSLog(@"%@", obj);    }];

这么一行就解决了,实在是太容易了.

要对什么对象排序就用相应的对象接收就行了:)

是不是简单过头了呢.

请记住,用block排序是最简单的方式!

下面来试试sortedArrayUsingDescriptors:这个方法.

sortedArrayUsingDescriptors:一般用来给Model进行排序,block也能对Model进行排序.先给出Model的定义(看教程不要太懒,自己敲代码吧)

以下是排序的代码:

////  AppDelegate.m//  Sort////  http://www.cnblogs.com/YouXianMing/////  Copyright (c) 2014年 Y.X. All rights reserved.//#import "AppDelegate.h"#import "Model.h"@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];    [self sort];        self.window.backgroundColor = [UIColor whiteColor];    [self.window makeKeyAndVisible];    return YES;}- (void)sort{    // 原始数组    NSArray *array = @[[Model name:@"YouXianMing" age:@26 height:171],                       [Model name:@"XiaoQiu"     age:@27 height:170],                       [Model name:@"HaoQuShi"    age:@28 height:172],                       [Model name:@"JunGang"     age:@24 height:171],                       [Model name:@"KongMing"    age:@30 height:175],                       [Model name:@"GaoFuShuai"  age:@22 height:180]];        // 排序描述信息    NSSortDescriptor *sortDescriptor  = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];    NSArray          *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];    NSArray          *sortedArray     = [array sortedArrayUsingDescriptors:sortDescriptors];        // 打印排序信息    [sortedArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {        Model *tmp = obj;        NSLog(@"%@", tmp.name);    }];}@end

看下图,其实呢,NSSortDescriptor只是一个获取keyPath的工具,他能根据keyPath进行排序而已,仅此而已:)

看一下打印信息:

2014-07-01 09:09:43.563 Sort[86442:60b] GaoFuShuai
2014-07-01 09:09:43.565 Sort[86442:60b] HaoQuShi
2014-07-01 09:09:43.565 Sort[86442:60b] JunGang
2014-07-01 09:09:43.566 Sort[86442:60b] KongMing
2014-07-01 09:09:43.566 Sort[86442:60b] XiaoQiu
2014-07-01 09:09:43.567 Sort[86442:60b] YouXianMing

很easy吧.

这种东西还是封装成类目比较好的样子.

使用:

////  AppDelegate.m//  Sort////  http://www.cnblogs.com/YouXianMing/////  Copyright (c) 2014年 Y.X. All rights reserved.//#import "AppDelegate.h"#import "Model.h"#import "NSArray+YXSort.h"@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];    [self sort];        self.window.backgroundColor = [UIColor whiteColor];    [self.window makeKeyAndVisible];    return YES;}- (void)sort{    // 原始数组    NSArray *array = @[[Model name:@"YouXianMing" age:@26 height:171],                       [Model name:@"XiaoQiu"     age:@27 height:170],                       [Model name:@"HaoQuShi"    age:@28 height:172],                       [Model name:@"JunGang"     age:@24 height:171],                       [Model name:@"KongMing"    age:@30 height:175],                       [Model name:@"GaoFuShuai"  age:@22 height:180]];        // 排序    NSArray *sortedArray = [array sortedWithKeyPath:@"name" ascending:YES];        // 打印排序信息    [sortedArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {        Model *tmp = obj;        NSLog(@"%@", tmp.name);    }];}@end

一句话就能实现排序,多简单:),开发就得隐藏不必要的繁文缛节,减少不必要的干扰才是正道.

第三种方法sortedArrayUsingSelector:,也许是你最常用的方法,这个我就不讲了,我觉得太麻烦了,还得另外写一个比较的方法......

 

 

总结:

==本人倾向于这么用==

1. 优先用block排序

2. 用NSSortDescriptor的keyPath排序

3. 再不济请用sortedArrayUsingSelector:方法排序

 

附录:

用block对Model排序一样非常简单直白暴力,只需用Model接收对象就可以了.