1、数组种元素为 NSNumber 类型,间接对数组种的元素进行排序:
#pragma mark -- 数组排序办法(升序)- (void)arraySortASC{
// 数组排序
// 定义一个数字数组
NSArray *array = @[@(3),@(4),@(2),@(1)];
// 对数组进行排序
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 -- 数组排序办法(降序)- (void)arraySortDESC{
// 数组排序
// 定义一个数字数组
NSArray *array = @[@(3),@(4),@(2),@(1)];
// 对数组进行排序
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 -- 数组排序办法(乱序)- (void)arraySortBreak{
// 数组排序
// 定义一个数字数组
NSArray *array = @[@(3),@(4),@(2),@(1),@(5),@(6),@(0)];
// 对数组进行排序
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);
}
2、数组内元素为对象,依据对象的某一属性进行排序:
#pragma mark -- 数组排序办法(升序)- (void)arraySortASCWithKind:(KindType)kindType{
// 数组排序
// 定义一个数字数组
NSArray *array = [NSArray arrayWithArray:self.dataMuArr];
// 对数组进行排序
NSArray *result = [array sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {NSLog(@"%@~%@",obj1,obj2); //3~4 2~1 3~1 3~2
if ([obj1 isKindOfClass:[BATicker class]] && [obj2 isKindOfClass:[BATicker class]]) {
BATicker * ticker1 = obj1;
BATicker * ticker2 = obj2;
NSNumber * number1 = @(ticker1.c);
NSNumber * number2 = @(ticker2.c);
if (kindType == KindTypeTurnover) {// 成交额
number1 = @(ticker1.q);
number2 = @(ticker2.q);
}else if (kindType == KindTypePrice) {// 价格
number1 = @(ticker1.c);
number2 = @(ticker2.c);
}else if (kindType == KindTypePercentageGain) {// 涨幅
number1 = @(ticker1.price_P);
number2 = @(ticker2.price_P);
}
return [number1 compare:number2]; // 升序
}
return [obj1 compare:obj2]; // 升序
}];
NSMutableDictionary * keyIndexDic = [NSMutableDictionary dictionary];
for (int i = 0; i < result.count; i++) {BATicker * ticker = result[i];
[keyIndexDic setObject:@(i) forKey:ticker.s];
}
self.dataMuArr = [NSMutableArray arrayWithArray:result];
self.keyIndexDic = [NSMutableDictionary dictionaryWithDictionary:keyIndexDic];
[self.tableView reloadData];
NSLog(@"result=%@",result);
}
#pragma mark -- 数组排序办法(降序)- (void)arraySortDESCKind:(KindType)kindType{
// 数组排序
// 定义一个数字数组
NSArray *array = [NSArray arrayWithArray:self.dataMuArr];
// 对数组进行排序
NSArray *result = [array sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {NSLog(@"%@~%@",obj1,obj2); //3~4 2~1 3~1 3~2
if ([obj1 isKindOfClass:[BATicker class]] && [obj2 isKindOfClass:[BATicker class]]) {
BATicker * ticker1 = obj1;
BATicker * ticker2 = obj2;
NSNumber * number1 = @(ticker1.c);
NSNumber * number2 = @(ticker2.c);
if (kindType == KindTypeTurnover) {// 成交额
number1 = @(ticker1.q);
number2 = @(ticker2.q);
}else if (kindType == KindTypePrice) {// 价格
number1 = @(ticker1.c);
number2 = @(ticker2.c);
}else if (kindType == KindTypePercentageGain) {// 涨幅
number1 = @(ticker1.price_P);
number2 = @(ticker2.price_P);
}
return [number2 compare:number1]; // 降序
}
return [obj2 compare:obj1]; // 降序
}];
NSMutableDictionary * keyIndexDic = [NSMutableDictionary dictionary];
for (int i = 0; i < result.count; i++) {BATicker * ticker = result[i];
[keyIndexDic setObject:@(i) forKey:ticker.s];
}
self.dataMuArr = [NSMutableArray arrayWithArray:result];
self.keyIndexDic = [NSMutableDictionary dictionaryWithDictionary:keyIndexDic];
[self.tableView reloadData];
NSLog(@"result=%@",result);
}
参考内容:
iOS 之数组的排序(升序、降序及乱序)【https://www.cnblogs.com/xiao-…