首页 > 编程知识 正文

人类游乐场汉化补丁,人类一败涂地

时间:2023-05-04 07:55:38 阅读:241515 作者:2107

练习.按学号的升序和降序排列;按姓名的升序和降序排序,按年龄的升序和降序排列。
        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]);
        }


                           

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。