iOS中的NSArraySort(^块语法)

main.m

 
//    NSArray *array =@[@"aa",@"ee",@"jj",@"mm",@"ss",@"kk"];
//    NSLog(@"%@",array);
//    NSArray *sortArray = [array sortedArrayUsingSelector:@selector(compare:)];
//     NSLog(@"%@",sortArray);
//
  
//    Student *stu1 = [Student studentWithName:@"chi" age:15 score:45.5 stuNum:@"1234"];
//     Student *stu2 = [Student studentWithName:@"niuniu" age:21 score:41 stuNum:@"34435"];
//     Student *stu3 = [Student studentWithName:@"paopao" age:33 score:22 stuNum:@"2122334"];
//     Student *stu4 = [Student studentWithName:@"doudou" age:5 score:99 stuNum:@"6172354"];
//     NSArray *array =@[stu1,stu2,stu3,stu4];
//      NSLog(@"%@",array);
//     NSArray *array1 = [array sortedArrayUsingSelector:@selector(compareByName:)];
//     NSLog(@"%@",array1);
//    
//    NSArray *array2 = [array sortedArrayUsingSelector:@selector(compareByAge:)];
//     NSLog(@"%@",array2);
//    NSArray *array3 = [array sortedArrayUsingSelector:@selector(compareByScore:)];
//    NSLog(@"%@",array3);
//    NSArray *array4 = [array sortedArrayUsingSelector:@selector(compareByStuNum:)];
//    NSLog(@"%@",array4);
//     NSMutableArray *arrayw =[@[stu1,stu2,stu3,stu4] mutableCopy];
//     [arrayw sortUsingSelector:@selector(compareByName:)];
//        NSLog(@"%@",arrayw);
    
    
    
    //block块语法
    //block 可以在函数内部定义匿名函数.
    //Block -- 实现求两个数的最大值
    //int (^)(int a, int b) Block类型
//    int (^maxBlock)(int a, int b) = ^int(int a, int b){
//    
//        return a > b ? a : b;
//    };
//    
//    
//    //如何掉用block
//    int max = maxBlock(3,5);
//    NSLog(@"%d", max);
//    //1.定义Block 输出 I love iOS!
//    void (^loveBlock)() = ^(){
//        NSLog(@"I love iOS !");
//    };
//    
//    //调用
//    loveBlock();
//    //2.定义一个block 将整形转为字符串
//    
//    NSString* (^zhuanhuanBloc)(int a) = ^NSString* (int a)
//    {
//      
//        return [NSString stringWithFormat:@"%d",a ];
//    
//    };
//    
//    
//    
//    
//    zhuanhuanBloc(4);
//    
//    //3.定义 BlocK实现将一个整形数转换成NSNumber对象.
//    NSNumber* (^nsnumberBlock)(int a) = ^NSNumber* (int a)
//    {
//        
//        //return @(a);
//     
//      return   [NSNumber numberWithInt:a];
//        
//    };
//    
//    //nsnumberBlock(3);
//    NSLog(@"%@",nsnumberBlock(3));
//    
//    //4.定义Block传入字符串,放回全部大写之后的字符串对象
//    
//    NSString *(^bigBlock)(NSString *a) = ^NSString *(NSString *a){
//       return [a uppercaseString];
//    };
//    
//    NSLog(@"%@",bigBlock(@"jfdlskafk"));
//    
//    //5.定义Block 传入字符串,返回首字母大写之后的字符串对象
//    typedef NSString *(^FistBigBlock)(NSString *a);
//    FistBigBlock fist = ^NSString *(NSString *a){
//        return [a capitalizedString];
//    };
//    
//    NSLog(@"%@",fist(@"fdsaf"));
//    //6.定义 Blcok实现两个字符串对象的比较
//    typedef NSComparisonResult(^BijiaoBlock)(NSString *a,NSString *b);
//    
//    BijiaoBlock bijiao = ^NSComparisonResult(NSString *a,NSString *b){
//       return [a compare:b];
//    };
//    if (bijiao(@"fdsa",@"fdsa") == 0) {
//        NSLog(@"相等");
//    }
//
    //块排序
//    NSArray *array1 = @[@"ss",@"zz",@"aa",@"yy"];
//   NSArray *bb = [array1 sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
//        NSString *str1 = obj1;
//        NSString *str2 = obj2;
//       return [str1 compare:str2];
//    }];
//    NSLog(@"%@",bb);
    //创建不可变数组,管理学生对象
        Student *stu1 = [Student studentWithName:@"chi" age:15 score:45.5 stuNum:@"1234"];
         Student *stu2 = [Student studentWithName:@"niuniu" age:21 score:41 stuNum:@"34435"];
         Student *stu3 = [Student studentWithName:@"paopao" age:33 score:22 stuNum:@"2122334"];
         Student *stu4 = [Student studentWithName:@"doudou" age:5 score:99 stuNum:@"6172354"];
         NSArray *array =@[stu1,stu2,stu3,stu4];
   NSArray *array1 = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        Student *stu1 = (Student *)obj1;
        Student *stu2 = (Student *)obj2;
        return [[stu1 name] compare: [stu2 name]];
    }];
    NSLog(@"%@",array1);
    //创建可变数,管理上述学生
    
    NSMutableArray *mArray = [@[stu1,stu2,stu3,stu4] mutableCopy];
    NSLog(@"%@",mArray);
    //可变数组和数组的快速排序的方法不同一个是sortUsingComparator一个是sortedArrayUsingComprator
   [mArray sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
            Student *stu1 = (Student *)obj1;
            Student *stu2 = (Student *)obj2;

        //NSInteger age1 = [self age];
      //  NSInteger age2 = [stu age];
        return -[@([stu1 score]) compare: @([stu2 score])];
    }];
    NSLog(@"%@",mArray);

Student.h

@public
    NSString *_name;
    NSInteger _age;
    CGFloat _score;
    NSString *_stuNum;
}

- (NSString *)name;
- (NSInteger)age;
- (CGFloat)score;
- (NSString *)stuNum;

- (id)initWithName:(NSString *)name age:(NSInteger)age score:(CGFloat)score stuNum:(NSString *)stuNum;
+ (id)studentWithName:(NSString *)name age:(NSInteger)age score:(CGFloat)score stuNum:(NSString *)stuNum;


//按姓名升序
- (NSComparisonResult)compareByName:(Student *)stu;

//年龄降序
- (NSComparisonResult)compareByAge:(Student *)stu;
//学号对象
- (NSComparisonResult)compareByStuNum:(Student *)stu;
//分数降序
- (NSComparisonResult)compareByScore:(Student *)stu;

Student.m

@implementation Student

- (NSString *)name
{
    return _name;
}
- (NSInteger)age
{
    return _age;
}
- (CGFloat)score
{
    return _score;
}
- (NSString *)stuNum
{
    return _stuNum;
}
- (id)initWithName:(NSString *)name age:(NSInteger)age score:(CGFloat)score stuNum:(NSString *)stuNum
{
    if (self = [super init]) {
        _name = name;
        _age = age;
        _score = score;
        _stuNum = stuNum;
    }
    return self;
}
+ (id)studentWithName:(NSString *)name age:(NSInteger)age score:(CGFloat)score stuNum:(NSString *)stuNum
{
    Student *stu = [[Student alloc] initWithName:name age:age score:score stuNum:stuNum];
    return stu;
}

//按姓名升序
- (NSComparisonResult)compareByName:(Student *)stu
{

    NSString *name1 = [self name];
    NSString *name2 = [stu name];
    return [name1 compare:name2];

}

//年龄降序
- (NSComparisonResult)compareByAge:(Student *)stu
{
    NSInteger age1 = [self age];
    NSInteger age2 = [stu age];
    return -[@(age1) compare: @(age2)];
}
//学号对象
- (NSComparisonResult)compareByStuNum:(Student *)stu
{
    return [[self stuNum] compare:[stu stuNum]];
}
//分数降序
- (NSComparisonResult)compareByScore:(Student *)stu
{
    NSInteger score1 = [self score];
    NSInteger score2 = [stu score];
    return -[@(score1) compare: @(score2)];
}
- (NSString *)description
{
    return [NSString stringWithFormat:@"%@,  %ld,   %@,    %.2f", _name, _age, _stuNum, _score];
}

 

转载于:https://www.cnblogs.com/wohaoxue/p/4723645.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值