使用NSArray类别里面的方法排序(排序方法二:sortUsingSelector:)

1.创立模型:Person类

#import <Foundation/Foundation.h>


@interface Person : NSObject

{

    NSString*_name;

    NSString*_sex;

    CGFloat _score;

    NSInteger _age;

}


-(void)setName:(NSString*)name;

-(void)setSex:(NSString*)sex;

-(void)setScore:(CGFloat)score;

-(void)setAge:(NSInteger)age;


-(NSString*)getName;

-(NSString*)getSex;

-(CGFloat)getScore;

-(NSInteger)getAge;


-(id)initWithName:(NSString*)name withSex:(NSString*)sex withScore:(CGFloat)score withAge:(NSInteger)age;


+(id)personWithName:(NSString*)name withSex:(NSString*)sex withScore:(CGFloat)score withAge:(NSInteger)age;



//自定义的类中重写排序方法

//外界只需要根据我们写好的排序规则排序即可


-(NSComparisonResult)compareWithAge:(Person*)person;


-(NSComparisonResult)compareWithName:(Person*)person;


@end


#import "Person.h"


@implementation Person

-(void)setName:(NSString*)name

{

    _name = name;

}

-(void)setSex:(NSString*)sex

{

    _sex = sex;

}

-(void)setScore:(CGFloat)score

{

    _score =score;

}

-(void)setAge:(NSInteger)age

{

    _age = age;

}


-(NSString*)getName

{

    return _name;

}

-(NSString*)getSex

{

    return _sex;

}

-(CGFloat)getScore

{

    return _score;

}

-(NSInteger)getAge

{

    return _age;

}


-(id)initWithName:(NSString*)name withSex:(NSString*)sex withScore:(CGFloat)score withAge:(NSInteger)age

{

    if (self = [super init]) {

        _name = name;

        _sex = sex;

        _age = age;

        _score = score;

    }

    return self;

}


+(id)personWithName:(NSString *)name withSex:(NSString *)sex withScore:(CGFloat)score withAge:(NSInteger)age

{

    Person*p = [[Person alloc]initWithName:name withSex:sex withScore:score withAge:age];

    return p;

    

}


- (NSString *)description

{

    return [NSString stringWithFormat:@"%@ %@ %0.2f %ld", _name,_sex,_score,_age];

}



#warning NSOrderedDescending是触发交换的条件(即某个判断条件下触发NSOrderedDescending则发生交换)

-(NSComparisonResult)compareWithAge:(Person*)person

{

    if (_age>[person getAge] ) {

        

        return NSOrderedDescending;

        

    }else if (_age<[person getAge]){

        

        return NSOrderedAscending;

    }

    return NSOrderedSame;


}



-(NSComparisonResult)compareWithName:(Person*)person

{

    if ([_name compare:[person getName]] ==YES) {

        

        return NSOrderedDescending;

        

    }else if ([_name compare:[person getName]]==NO){

        

        return NSOrderedAscending;

    }

    return NSOrderedSame;



}


@end



2.外面调用实现排序

Person*per1 = [Person personWithName:@"xueheng" withSex:@"male" withScore:92 withAge:25];

     Person*per2 = [Person personWithName:@"zhanglizhi" withSex:@"male" withScore:88 withAge:27];

     Person*per3 = [Person personWithName:@"liyanlie" withSex:@"male" withScore:87 withAge:23];

     Person*per4 = [Person personWithName:@"wanghelin" withSex:@"male" withScore:91 withAge:26];

     Person*per5 = [Person personWithName:@"mingming" withSex:@"male" withScore:76 withAge:22];

     Person*per6 = [Person personWithName:@"aili" withSex:@"famale" withScore:82 withAge:25];

    

    //不可变数组排序返回一个新的数组

    NSMutableArray*array = [NSMutableArray arrayWithObjects:per1,per2,per3,per4,per5,per6, nil];

    

    //自定义compare

    [array sortUsingSelector:@selector(compareWithName:)];

    NSLog(@"%@",array);





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值