NSArray *array = @[@(3),@(4),@(1),@(2),@(5)]; //升序 NSArray *array1 = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) { return [obj1 compare:obj2]; }]; NSLog(@"%@",array1); //降序 NSArray *array2 = [array sortedArrayUsingComparator:^NSComparisonResult(NSNumber *obj1,NSNumber *obj2) { //相对于升序,compare参数调换一下 return [obj2 compare:obj1]; }]; NSLog(@"%@",array2); //乱序 NSArray *array3 = [array sortedArrayUsingComparator:^NSComparisonResult(NSNumber *obj1,NSNumber *obj2) { //0或者1的随机数 int i = arc4random_uniform(2); if (i) { return [obj1 compare:obj2]; }else{ return [obj2 compare:obj1]; } }]; NSLog(@"%@",array3);