pragma mark -- 數(shù)組排序方法(升序)
- (void)arraySortASC{
//數(shù)組排序
//定義一個(gè)數(shù)字?jǐn)?shù)組
NSArray *array = @[@(3),@(4),@(2),@(1)];
//對(duì)數(shù)組進(jìn)行排序
NSArray *result = [array sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
NSLog(@"%@~%@",obj1,obj2); //3~4 2~1 3~1 3~2
return [obj1 compare:obj2]; //升序
}];
NSLog(@"result=%@",result);
}
pragma mark -- 數(shù)組排序方法(降序)
- (void)arraySortDESC{
//數(shù)組排序
//定義一個(gè)數(shù)字?jǐn)?shù)組
NSArray *array = @[@(3),@(4),@(2),@(1)];
//對(duì)數(shù)組進(jìn)行排序
NSArray *result = [array sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
NSLog(@"%@~%@",obj1,obj2); //3~4 2~1 3~1 3~2
return [obj2 compare:obj1]; //降序
}];
NSLog(@"result=%@",result);
}
pragma mark -- 數(shù)組排序方法(亂序)
- (void)arraySortBreak{
//數(shù)組排序
//定義一個(gè)數(shù)字?jǐn)?shù)組
NSArray *array = @[@(3),@(4),@(2),@(1),@(5),@(6),@(0)];
//對(duì)數(shù)組進(jìn)行排序
NSArray *result = [array sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
NSLog(@"%@~%@",obj1,obj2);
//亂序
if (arc4random_uniform(2) == 0) {
return [obj2 compare:obj1]; //降序
}
else{
return [obj1 compare:obj2]; //升序
}
}];
NSLog(@"result=%@",result);
}