字符串之间比较大小(可转化为对象)

来源:互联网 发布:数据可视化软件 ta 编辑:程序博客网 时间:2024/05/29 07:16

Student *stu1=[[Studentalloc] init];

        stu1.name=@"xiaolongxia";

        stu1.age=18;

       Student *stu2=[Studentnew];

        stu2.name=@"dazhaxie";

        stu2.age=23;

       NSMutableArray *mularr=[NSMutableArrayarrayWithObjects:stu1,stu2, nil];

        

       //第一种比较

        [mularrsortUsingSelector:@selector(compareWithName:)];

       for (int i=0; i<mularr.count; i++) {

           NSLog(@"%@",mularr[i]);

        }

        [mularrsortUsingSelector:@selector(compareWithAge:)];

       for (int i=0; i<mularr.count; i++) {

           NSLog(@"%@",mularr[i]);

        }

        

       //第二种

        [mularrsortUsingComparator:^NSComparisonResult(Student *obj1,Student *obj2) {

           return  [obj1 compareWithName:obj2];

        }];

       for (id objin mularr) {

           NSLog(@"%@",obj);

        }

        [mularrsortUsingComparator:^NSComparisonResult(Student *obj1,Student *obj2) {

           int result;

           if (obj1.age>obj2.age) {

                result=NSOrderedDescending;

            }elseif(obj1.age<obj2.age){

                result=NSOrderedAscending;

            }else {

                result=NSOrderedSame;

            }

           return result;

        }];

       for (id objin mularr) {

           NSLog(@"%@",obj);

        }

        

       //第三种

        NSSortDescriptor *desc=[NSSortDescriptorsortDescriptorWithKey:@"name"ascending:YES];

        [mularrsortUsingDescriptors:@[desc]];

       NSEnumerator *e=[mularr objectEnumerator];

       NSLog(@"%@",[eallObjects]);

        

        NSSortDescriptor *desc1=[NSSortDescriptorsortDescriptorWithKey:@"age"ascending:YES];

        [mularrsortUsingDescriptors:@[desc1]];

       NSEnumerator *ee=[mularr objectEnumerator];

       NSLog(@"%@",[eeallObjects]);

        


0 0
原创粉丝点击