NSArray *stringArray = [NSArray arrayWithObjects:@"zhangsan",@"lisi",@"wangjun",@"lanlan",@"yangyi",@"qianzhongshu",@"sunwukong",@"wugang", nil];
NSArray *sortedArray = [stringArray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [obj1 compare:obj2];//顺序
return -[obj1 compare:obj2];//倒序(排序结果为枚举值[-1,0,1].所以想要得到排序结果在控制台倒序输出,加一个"-"即可。
}];
NSLog(@"%@",sortedArray);
Objective-C之Block与数组排序
时间: 2024-12-04 22:06:22