- (void)arraySortASC{
//数组排序
NSArray *array = @[@"3",@"4",@"2",@"1",@"8",@"7"];
//对数组进行排序
NSArray *result = [array sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
NSLog(@"%@~%@",obj1,obj2);
return [obj1 compare:obj2];//升序
}];
NSLog(@"result=%@",result);
}
#PRagma mark -- 数组排序方法(降序)
- (void)arraySortDESC{
//数组排序
NSArray *array = @[@"3",@"4",@"2",@"1",@"8",@"7"];
//对数组进行排序
NSArray *result = [array sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
NSLog(@"%@~%@",obj1,obj2);
return [obj2 compare:obj1];//降序
}];
NSLog(@"result=%@",result);
}
#pragma mark -- 数组排序方法(乱序)
- (void)arraySortBreak{
//数组排序
//定义一个数字数组
NSArray *array = @[@"3",@"4",@"2",@"1",@"8",@"7"];
//对数组进行排序
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);
}
新闻热点
疑难解答