Range objects array by NSComparisonResult。(通过NSComparisonResult对NSArray排序)

来源:互联网 发布:中国大学生就业数据 编辑:程序博客网 时间:2024/05/17 09:15

  NSMutableArray *testArray = [[NSMutableArrayalloc]init];

    for (int i = 0; i<10; i++) {

        Test *testEle = [[Testalloc]init];

        testEle.numberStr = [NSStringstringWithFormat:@"序号是%d",i];

        [testArray addObject:testEle];

        [testEle release];

    }

    NSComparisonResult(^cmptr)(id obj1,id obj2) = ^NSComparisonResult(Test *obj1,Test *obj2) {

        if ([obj1.numberStrcompare:obj2.numberStr]) {

            returnNSOrderedDescending;

        } else {

            returnNSOrderedAscending;

        }

    };

    for (int i=0; i<10; i++) {

        NSLog(@"before:%@",((Test *)[testArrayobjectAtIndex:i]).numberStr);

    }

    

    [testArray sortUsingComparator:cmptr];

    for (int i=0; i<10; i++) {

        NSLog(@"after:%@",((Test *)[testArrayobjectAtIndex:i]).numberStr);

    }