1 NSArray *array = @[@(3),@(4),@(1),@(2),@(5)]; 2 //升序 3 NSArray *array1 = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) { 4 return [obj1 compare:obj2]; 5 }]; 6 NSLog(@"%@",array1); 7 8 //降序 9 NSArray *array2 = [array sortedArrayUsingComparator:^NSComparisonResult(NSNumber *obj1,NSNumber *obj2) { 10 //相对于升序,compare参数调换一下 11 return [obj2 compare:obj1]; 12 }]; 13 NSLog(@"%@",array2); 14 15 16 //乱序 17 NSArray *array3 = [array sortedArrayUsingComparator:^NSComparisonResult(NSNumber *obj1,NSNumber *obj2) { 18 //0或者1的随机数 19 int i = arc4random_uniform(2); 20 if (i) { 21 return [obj1 compare:obj2]; 22 }else{ 23 return [obj2 compare:obj1]; 24 } 25 26 }]; 27 NSLog(@"%@",array3);
原文:http://www.cnblogs.com/cgphp/p/4356335.html