美文网首页
数据排序(规则 如果有相同的姓,就比较名字)

数据排序(规则 如果有相同的姓,就比较名字)

作者: ZHANG_GO | 来源:发表于2018-08-29 18:33 被阅读12次

    https://blog.csdn.net/daiyelang/article/details/18726947

    #pragma mark 数组排序1

    voidarraySort1() {

    NSArray*array = [NSArrayarrayWithObjects:@"2",@"3",@"1",@"4",nil];

    // 返回一个排好序的数组,原来数组的元素顺序不会改变

    // 指定元素的比较方法:compare:

    NSArray*array2 = [array sortedArrayUsingSelector:@selector(compare:)];

    NSLog(@"array2:%@", array2);

    }

    #pragma mark 数组排序2

    voidarraySort2() {

    Student *stu1 = [Student studentWithFirstname:@"MingJie"lastname:@"Li"];

    Student *stu2 = [Student studentWithFirstname:@"LongHu"lastname:@"Huang"];

    Student *stu3 = [Student studentWithFirstname:@"LianJie"lastname:@"Li"];

    Student *stu4 = [Student studentWithFirstname:@"Jian"lastname:@"Xiao"];

    NSArray*array = [NSArrayarrayWithObjects:stu1,stu2,stu3, stu4,nil];

    // 指定排序的比较方法

    NSArray*array2 = [array sortedArrayUsingSelector:@selector(compareStudent:)];

    NSLog(@"array2:%@", array2);

    }

    - (NSComparisonResult)compareStudent:(Student *)stu {

    // 先按照姓排序

    NSComparisonResultresult = [self.lastname compare:stu.lastname];

    // 如果有相同的姓,就比较名字

    if(result ==NSOrderedSame) {

    result = [self.firstname compare:stu.firstname];

        }

    returnresult;

    }

    #pragma mark 数组排序3

    voidarraySort3() {

    Student *stu1 = [Student studentWithFirstname:@"MingJie"lastname:@"Li"];

    Student *stu2 = [Student studentWithFirstname:@"LongHu"lastname:@"Huang"];

    Student *stu3 = [Student studentWithFirstname:@"LianJie"lastname:@"Li"];

    Student *stu4 = [Student studentWithFirstname:@"Jian"lastname:@"Xiao"];

    NSArray*array = [NSArrayarrayWithObjects:stu1,stu2,stu3, stu4,nil];

    // 利用block进行排序

    NSArray*array2 = [array sortedArrayUsingComparator:

    ^NSComparisonResult(Student *obj1, Student *obj2) {

    // 先按照姓排序

    NSComparisonResultresult = [obj1.lastname compare:obj2.lastname];

    // 如果有相同的姓,就比较名字

    if(result ==NSOrderedSame) {

                result = [obj1.firstname compare:obj2.firstname];

            }

    returnresult;

        }];

    NSLog(@"array2:%@", array2);

    }

    #pragma mark 数组排序4-高级排序

    voidarraySort4() {

    Student *stu1 = [Student studentWithFirstname:@"MingJie"lastname:@"Li"bookName:@"book1"];

    Student *stu2 = [Student studentWithFirstname:@"LongHu"lastname:@"Huang"bookName:@"book2"];

    Student *stu3 = [Student studentWithFirstname:@"LianJie"lastname:@"Li"bookName:@"book2"];

    Student *stu4 = [Student studentWithFirstname:@"Jian"lastname:@"Xiao"bookName:@"book1"];

    NSArray*array = [NSArrayarrayWithObjects:stu1,stu2,stu3, stu4,nil];

    // 1.先按照书名进行排序

    // 这里的key写的是@property的名称

    NSSortDescriptor*bookNameDesc = [NSSortDescriptorsortDescriptorWithKey:@"book.name"ascending:YES];

    // 2.再按照姓进行排序

    NSSortDescriptor*lastnameDesc = [NSSortDescriptorsortDescriptorWithKey:@"lastname"ascending:YES];

    // 3.再按照名进行排序

    NSSortDescriptor*firstnameDesc = [NSSortDescriptorsortDescriptorWithKey:@"firstname"ascending:YES];

    // 按顺序添加排序描述器

    NSArray*descs = [NSArrayarrayWithObjects:bookNameDesc, lastnameDesc, firstnameDesc,nil];

    NSArray*array2 = [array sortedArrayUsingDescriptors:descs];

    NSLog(@"array2:%@", array2);

    }

    相关文章

      网友评论

          本文标题:数据排序(规则 如果有相同的姓,就比较名字)

          本文链接:https://www.haomeiwen.com/subject/oxikwftx.html