黑马程序员--数组常用排序方法


接口文件

//
//  Person.h
//  字符串练习
//
//  Created by smartlei on 15/6/12.
//  Copyright (c) 2015年 smartlei. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Person : NSObject


@property (nonatomic,assign) NSString *name;
@property (nonatomic,assign) int record;
@property (nonatomic,assign) int age;


-(Person *)initWithName:(NSString *)name andRecord:(int)record andAge:(int)age;
-(NSComparisonResult) compareName:(Person *)element;
@end

实现文件

//
//  Person.m
//  字符串练习
//
//  Created by smartlei on 15/6/12.
//  Copyright (c) 2015年 smartlei. All rights reserved.
//

#import "Person.h"

@implementation Person



@synthesize name;
@synthesize record;
@synthesize age;
//便利构造器
-(Person *)initWithName:(NSString *)name andRecord:(int)record andAge:(int)age
{
    if (self=[super init])
    {
        self->name=name;
        self->age=age;
        self->record=record;
    }
    return self;
    
}
//重写打印方法
-(NSString *)description
{
    return [NSString stringWithFormat:@"%@ %d %d",name,age,record];
}
//排序:只能排序字符串
-(NSComparisonResult) compareName:(Person *)element
{
    return [name compare: element.name];
}
@end


测试文件

//
//  main.m
//  练习多种方式数组排序
//
//  Created by smartlei on 15/6/12.
//  Copyright (c) 2015年 smartlei. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Person.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
      
        Person *p1=[[Person alloc] initWithName:@"zhang" andRecord:56 andAge:17];
        
        Person *p2=[[Person alloc] initWithName:@"gao" andRecord:78 andAge:19];
        
        Person *p3=[[Person alloc] initWithName:@"gao" andRecord:56 andAge:34];
        
        Person *p4=[[Person alloc] initWithName:@"sun" andRecord:90 andAge:45];
        Person *p5=[[Person alloc] initWithName:@"gao" andRecord:90 andAge:34];
        
        
        NSArray *arry=[NSArray arrayWithObjects:p1,p2,p3,p4,p5, nil];
  
        
        /****************按照描述符排序*******************/
        NSLog(@"*************************************");
        //排序描述符
        NSSortDescriptor *nameDescriptor=[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];//升序
        NSSortDescriptor *ageDescriptor=[NSSortDescriptor sortDescriptorWithKey:@"age" ascending:YES];//年龄降序
        NSSortDescriptor *recordDescriptor=[NSSortDescriptor sortDescriptorWithKey:@"record" ascending:YES];
        
        
        //把需要排序的描述符放到数组中
        
        NSArray *descriptorArr=[NSArray arrayWithObjects:nameDescriptor,ageDescriptor,recordDescriptor,nil];
        
        NSArray *sortedarr=[arry sortedArrayUsingDescriptors:descriptorArr];
    
        for(Person *element in sortedarr)
        {
            NSLog(@"%@",element);

        }
        
        /****************按照selector*******************/
        NSLog(@"*************************************");
        NSArray *selecotArr=[arry sortedArrayUsingSelector:@selector(compareName:)];
        
        for(Person *element in selecotArr)
        {
            NSLog(@"%@",element);
            
        }
        
        /****************按照代码块排序*******************/
        NSLog(@"*************************************");
        NSArray *blockArr=[arry sortedArrayUsingComparator:^(id obj1,id obj2){return [obj1 compareName:obj2];}];
        
        for(Person *element in blockArr)
        {
            NSLog(@"%@",element);
            
        }
        
    }
    return 0;
}

输出:

2015-06-12 17:23:42.266 练习多种方式数组排序[919:35712] *************************************

2015-06-12 17:23:42.268 练习多种方式数组排序[919:35712] gao 19 78

2015-06-12 17:23:42.268 练习多种方式数组排序[919:35712] gao 34 56

2015-06-12 17:23:42.268 练习多种方式数组排序[919:35712] gao 34 90

2015-06-12 17:23:42.268 练习多种方式数组排序[919:35712] sun 45 90

2015-06-12 17:23:42.269 练习多种方式数组排序[919:35712] zhang 17 56

2015-06-12 17:23:42.269 练习多种方式数组排序[919:35712] *************************************

2015-06-12 17:23:42.269 练习多种方式数组排序[919:35712] gao 19 78

2015-06-12 17:23:42.269 练习多种方式数组排序[919:35712] gao 34 56

2015-06-12 17:23:42.269 练习多种方式数组排序[919:35712] gao 34 90

2015-06-12 17:23:42.269 练习多种方式数组排序[919:35712] sun 45 90

2015-06-12 17:23:42.270 练习多种方式数组排序[919:35712] zhang 17 56

2015-06-12 17:23:42.270 练习多种方式数组排序[919:35712] *************************************

2015-06-12 17:23:42.270 练习多种方式数组排序[919:35712] gao 19 78

2015-06-12 17:23:42.270 练习多种方式数组排序[919:35712] gao 34 56

2015-06-12 17:23:42.276 练习多种方式数组排序[919:35712] gao 34 90

2015-06-12 17:23:42.277 练习多种方式数组排序[919:35712] sun 45 90

2015-06-12 17:23:42.277 练习多种方式数组排序[919:35712] zhang 17 56






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值