NSArray类中的sortedArrayUsingSelector实例对象方法----详解

    在API文档中会发现每个类的介绍是很详细的,比如NSArray类的方法会被分成很多类别,而在Sorting类别中就有sortedArrayUsingSelector:方法。

    该方法的完整代码是  - (NSArray *)sortedArrayUsingSelector:(SEL)comparator;

意思是:给你一个数组,对这个数组中的对象进行排序(默认升序),然后将排序的对象放在一个新数组中。为什么要放在一个新数组中呢?因为NSArray是一个不可变数组,不能在原数组中进行操作,所以需要将排好序的对象依次放在新数组中。

代码举例:

        定义一个数组array:如下

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

其中的  @"1",@"3",@"2",@"0"  是我初始化的字符串对象(NSArray中只能放对象,不能放基本数据类型),nil 是结束标志。

        NSArray *sortedArray1 = [array sortedArrayUsingSelector:@selector(compare:)];

        NSLog(@"%@", sortedArray1);

// compare:NSString自带的对象方法。在这就有点明白了 参数(SEL)comparator 要填写的内容啦。参数中放的是一个对象方法,这个对象方法的选择是有根据的,它根据数组array中存储的数据是什么类型的,在上例中存储的类型是NSString,所以参数中存放对象方法就是NSString类中的compare:方法。

      结果:2014-03-28 18:37:54.942 Lesson5.1[2000:303] (

                          0,

                          1,

                          2,

                          3

               )

到这里,我们知道了怎么使用该方法啦,其实就是一个封装好的排序方法!

下面再用 自定义类 的方法实现- (NSArray *)sortedArrayUsingSelector:(SEL)comparator;的使用。

首先在.h文件中声明一个类:

@interface Person : NSObject

{

    NSString  *_name;

    CGFloat _height;

    CGFloat  _score;

}

- (id)initWithName:(NSString *)name andHeight:(CGFloat)height andScore:(CGFloat)score;

- (NSComparisonResult)compareHeight:(Person *)anthorPerson;

- (NSComparisonResult)compareScore:(Person *)anthorPerson;

- (CGFloat)getHeight;

- (CGFloat)getScore;

- (NSString *)description;

- (void)sayHi;

@end

 在.m文件中实现:

@implementation Person

- (id)initWithName:(NSString *)name andHeight:(CGFloat)height andScore:(CGFloat)score{

    self = [super init];

    if (self) {

        _name = name;

        _height = height;

        _score = score;

    }

    return  self;

}

- (NSComparisonResult)compareHeight:(Person *)anthorPerson{

    if (_height > [anthorPerson getHeight]) {

        returnNSOrderedDescending;

    }

    else if (_height == [anthorPerson getHeight]){

        returnNSOrderedSame;

    }else{

        returnNSOrderedAscending;

    }

}

- (NSComparisonResult)compareScore:(Person *)anthorPerson{

    if (_score > [anthorPerson getScore]) {

        returnNSOrderedDescending;

    }

    else if (_score == [anthorPerson getScore]){

        returnNSOrderedSame;

    }else{

        returnNSOrderedAscending;

    }

}

- (void)sayHi{

    NSLog(@"hello");

}

- (CGFloat)getHeight{

    return_height;

}

- (CGFloat)getScore{

    return _score;

}

- (NSString *)description{

    NSString *des = [NSStringstringWithFormat:@"name:%@,height:%.1f,score:%.1f",_name,_height,_score];

    return des;

}

@end

 上面两步完成后,在main.m文件中实现:

 @autoreleasepool {

        // 定义4个person对象:

        Person *person1 = [[Person alloc] initWithName:@"zbw" andHeight:175.2 andScore:87];

        Person *person2 = [[Person alloc] initWithName:@"wxm" andHeight:173.2 andScore:97];

        Person *person3 = [[Person alloc] initWithName:@"hxe" andHeight:170.2 andScore:37];

        Person *person4 = [[Person alloc] initWithName:@"wei" andHeight:163.2 andScore:67];

       // 将这四个对象放到array数组中:如下 

       NSArray *array = [NSArray arrayWithObjects:person1,person2,person3,person4, nil];

       // compareScore是Person类的对象方法(该方法在上面的.m中可以看到) 

        NSArray *sortedArray = [array sortedArrayUsingSelector:@selector(compareScore:)];

        NSLog(@"%@", sortedArray);// 输出打印

        注意: 数组中如果放的是字符串,排序的时候使用字符串的方法(compare:) 如果放的是自定义Person对象,排序的时候使用Person类的方法(compareScore)。

       好啦,到这里就结束啦。

       

       

原文地址:https://www.cnblogs.com/zbw-fly/p/3631332.html