在block按学号的升序和降序排列;按姓名的升序和降序排序,按年龄的升序和降序排列

练习.按学号的升序和降序排列;按姓名的升序和降序排序,按年龄的升序和降序排列。
        Student *stu1 = [Student studentWithName:@"xiaoguo"
                                         withSex:@"女"
                                         withAge:120
                                    andStunumber:689];
        Student *stu2 = [Student studentWithName:@"yanmin"
                                         withSex:@"女"
                                         withAge:121
                                    andStunumber:488];
        Student *stu3 = [Student studentWithName:@"Qinzhen"
                                         withSex:@"女"
                                         withAge:129
                                    andStunumber:267];
        Student *stu4 = [Student studentWithName:@"zhanxiao"
                                         withSex:@"男"

                                         withAge:222

                                   andStunumber:534];
        Student *stu5 = [Student studentWithName:@"Guagua"
                                         withSex:@"男"
                                         withAge:125
                                    andStunumber:765];
        
        NSMutableArray *students = [NSMutableArray arrayWithObjects:stu1,stu2,stu3,stu4,stu5, nil];
        
  (1)按姓名排序
        
        [students sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
            //(Student *)强制类型转换的标志,obj1是id类型。当我们使用时可以强转称所需要的类型
            Student *stu1 = (Student *)obj1;
            Student *stu2 = (Student *)obj2;
            
 下面是按姓名升序排。若按姓名的降序排,只需将前两个返回值换一下,起控制作用的是最外层的判断和返回值。
            if ([[stu1 name] compare: [stu2 name]] < 0) {
                return NSOrderedAscending;
            } else if ([[stu1 name] compare:[stu2 name]] > 0){
                return NSOrderedDescending;
            } else {
                return NSOrderedSame;
            }
            
        }];
        
        
   遍历数组,查看内容
        for (Student *stu in students) {
            NSLog(@"stu.name = %@",[stu name]);
        }
        
        
 (2)按年龄排序
        
        [students sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
 //(Student *)强制类型转换的标志,obj1是id类型。当我们使用时可以强转称所需要的类型
            Student *stu1 = (Student *)obj1;
            Student *stu2 = (Student *)obj2;
 按年龄排序,如果第一个对象的年龄 小于 第二个对象的年龄,就按照升序排列,如果大于,就是降序排列
        if ([stu1 age] < [stu2 age]) {
            return NSOrderedAscending;
        } else if ([stu1 age] > [stu2 age]){
            return NSOrderedDescending;
        } else {
            return NSOrderedSame;
        }
      }];
    //遍历数组,查看内容
        for (Student *stu in students) {
          NSLog(@"stu.age = %lu",[stu age]);
      }
    
   //(3)按学号排序
        
        [students sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
            //(Student *)强制类型转换的标志,obj1是id类型。当我们使用时可以强转称所需要的类型
            Student *stu1 = (Student *)obj1;
            Student *stu2 = (Student *)obj2;
            //按学号排序,如果第一个对象的学号 小于 第二个对象的学号,就按照升序排列,如果大于,就是降序排列
            for (Student *stu in students) {
                NSLog(@"stu.number = %lu",[stu stunumber]);
            }
            if ([stu1 stunumber] < [stu2 stunumber]) {
                return NSOrderedAscending;
            } else if ([stu1 stunumber] > [stu2 stunumber]){
                return NSOrderedDescending;
            } else {
                return NSOrderedSame;
            }
            
        }];
        //遍历数组,查看内容
        for (Student *stu in students) {
            NSLog(@"stu.number = %lu",[stu stunumber]);
        }


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值