NSMutableArray *testArray = [[NSMutableArray alloc]init];
for (int i = 0; i<10; i++) {
Test *testEle = [[Test alloc]init];
testEle.numberStr = [NSString stringWithFormat:@"序号是%d",i];
[testArray addObject:testEle];
[testEle release];
}
NSComparisonResult(^cmptr)(id obj1, id obj2) = ^NSComparisonResult(Test *obj1, Test *obj2) {
if ([obj1.numberStr compare:obj2.numberStr]) {
return NSOrderedDescending;
} else {
return NSOrderedAscending;
}
};
for (int i=0; i<10; i++) {
NSLog(@"before:%@",((Test *)[testArray objectAtIndex:i]).numberStr);
}
[testArray sortUsingComparator:cmptr];
for (int i=0; i<10; i++) {
NSLog(@"after:%@",((Test *)[testArray objectAtIndex:i]).numberStr);
}