码迷,mamicode.com
首页 > 其他好文 > 详细

NSArray排序方法讲解

时间:2014-07-01 19:15:07      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   使用   

NSArray排序方法讲解

bubuko.com,布布扣

给数组排序有着多种方式

bubuko.com,布布扣

最麻烦的是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);
    }];

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

bubuko.com,布布扣

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

bubuko.com,布布扣

是不是简单过头了呢.

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

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

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

bubuko.com,布布扣

bubuko.com,布布扣

以下是排序的代码:

//
//  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进行排序而已,仅此而已:)

bubuko.com,布布扣

看一下打印信息:

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吧.

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

bubuko.com,布布扣

bubuko.com,布布扣

使用:

//
//  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

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

bubuko.com,布布扣

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

 

 

总结:

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

1. 优先用block排序

2. 用NSSortDescriptor的keyPath排序

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

 

附录:

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

bubuko.com,布布扣

 

 

 

NSArray排序方法讲解,布布扣,bubuko.com

NSArray排序方法讲解

标签:des   style   blog   http   color   使用   

原文地址:http://www.cnblogs.com/YouXianMing/p/3817656.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!