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接收对象就可以了.

NSArray排序方法讲解

时间: 2024-08-27 07:36:21

NSArray排序方法讲解的相关文章

NSArray排序方法

sortedArrayUsingComparator: NSMutableArray *array = [NSMutableArray arrayWithObjects: [NSDictionary dictionaryWithObjectsAndKeys:@"Obj0", [NSNumber numberWithInt:0], nil], [NSDictionary dictionaryWithObjectsAndKeys:@"Obj5", [NSNumber n

NSArray 排序

NSArray排序方法讲解 给数组排序有着多种方式 最麻烦的是sortedArrayUsingSelector:,其次是sortedArrayUsingDescriptors:,最容易使用的就是sortedArrayUsingComparator: 从最容易使用的开始吧: // 原始数组 NSArray *array = @[@"b", @"a", @"x", @"o", @"g", @"o&qu

NSArray排序的一些方法

/* 大体上,OC中常用的数组排序有以下几种方法: sortedArrayUsingSelector: sortedArrayUsingComparator: sortedArrayUsingDescriptors: */ /* 1.简单排序(sortedArrayUsingSelector:) 如果只是对字符串的排序,可以利用sortedArrayUsingSelector:方法就可以了,代码如下 */ // 简单排序 void sortArray1(){ NSArray *array = [

【学习ios之路:Objective-C】OC中常用的系统排序方法

①.OC中常用排序方法: 1).不可变数组 - (NSArray *)sortedArrayUsingSelector:(SEL)comparator; - (NSArray *)sortedArrayUsingComparator:(NSComparator)cmptr; 2)可变数组 - (void)sortUsingSelector:(SEL)comparator; - (void)sortUsingComparator:(NSComparator)cmptr; 3).字典排序 - (NS

[OC Foundation框架 - 8] NSArray排序

1.派生 1 voidarrayNew() 2 { 3 NSArray*array = [NSArrayarrayWithObjects:@"1",@"2",nil]; 4 NSArray*array2 = [arrayarrayByAddingObject:@"3"]; 5 NSLog(@"%@", array2); 6 7 8 NSArray*array4 = [NSArrayarrayWithObjects:@"

关于NSArray的方法知识简单了解

简单分享下,希望大牛们多多指点迷津,逐步修改,完善不足支持.借鉴之处,还请谅解 #import <Foundation/Foundation.h> @interface Student : NSObject @property (nonatomic,assign) NSString *name; @property (nonatomic,assign) NSString *classes; @property (nonatomic,assign) int age; +(id)studentWi

iOS开发之排序方法比较

在开发应用程序的时候,有时我们需要对一组无序的内容进行排序,iOS中有系统自带的方法来对NSAray进行排序,我们来对这些方法进行性能上的对比: NSComparator排序 NSDescriptor排序 function排序 quickSort排序 由于排序的对象经常是自定义的,因此我们定义一个如下的对象: @interface Topic : NSObject @property (nonatomic, assign) NSInteger ID; @property (nonatomic,

对数组中的对象进行升序以及降序的排序方法(其中对象类型是系统类型).

[cpp] view plaincopyprint? <span style="font-size:32px;">NSArray *arr = [[NSArray alloc] initWithObjects:@"aa",@"bb",@"cc",@"dd",@"ee",@"ff", nil]; //1.对数组进行升序排序 //sortedArrayUsin

OC基础-NSArray排序

OC中的NSArray提供了较多的排序方法,可以对数组元素进行有效的排序,下面先准备一个Student和Course类来作为练习对象. 一 创建练习类 1 Course类 // Course.h #import <Foundation/Foundation.h> @interface Course : NSObject /** 初始化Course类的类方法*/ + (instancetype)courseWithEnglish:(float)englishRecords