【iOS笔记】【7】iOS数组排序

本文最后更新于:2021年12月22日 中午

【主页】系列文章目录

【iOS笔记】系列目录


简单说明

数组排序比较多,我们讲一种比较常用的,这里涉及到一个知识点:
NSComparisonResult:

1
2
3
4
5
typedef enum NSComparisonResult : NSInteger {
NSOrderedAscending = -1L,
NSOrderedSame,
NSOrderedDescending
} NSComparisonResult;

image.png
NSOrderedAscending: 左边数据比右边小,可理解成增序
NSOrderedSame: 相等
NSOrderedDescending: 左边数据比右边大,可理解成降序


示例:

1
NSArray *array = @[@"Benz",@"BMW",@"Ferrari",@"Lamborghini"];

1、普通排序系统自带的升序

1
2
NSArray *sortedArray1 = [array sortedArrayUsingSelector:@selector(compare:)];
NSLog(@"普通排序:%@",sortedArray1);

2、逆转数组

1
2
3
4
5
NSArray *sortedArray2 = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1,id obj2) {
//逆转
return NSOrderedDescending;
}];
NSLog(@"逆转数组:%@",sortedArray2);

3、不逆转(相当于原数组)

1
2
3
4
5
NSArray *sortedArray3 = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1,id obj2) {
//不逆转
return NSOrderedAscending;
}];
NSLog(@"不逆转(原数据):%@",sortedArray3);

4、升序排列

1
2
3
4
5
6
NSArray *sortedArray4 = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1,id obj2) {
//升序
NSComparisonResult result = [obj1 compare: obj2];
return result;
}];
NSLog(@"升序排列:%@",sortedArray4);

5、降序排列

1
2
3
4
5
6
NSArray *sortedArray5 = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
//降序
NSComparisonResult result = [obj1 compare: obj2];
return -result;
}];
NSLog(@"降序排列:%@",sortedArray5);

image.png


进阶:

数组里面存放模型,根据模型的某个属性值来对数组进行重新排序

1、初始化一些车辆和数组:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Car *benz = [Car new];
benz.name = @"Benz";
benz.price = 2000.0;

Car *bmw = [Car new];
bmw.name = @"BMW";
bmw.price = 800;

Car *ferrari = [Car new];
ferrari.name = @"Ferrari";
ferrari.price = 1200;

Car *lamborghini = [Car new];
lamborghini.name = @"Lamborghini";
lamborghini.price = 12000;

NSMutableArray *array = @[benz,bmw,ferrari,lamborghini].mutableCopy;

2、排序方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
对数组里存放model的某个属性对数组进行重新排序

@param array 待排序数组
@param increase 是否增序排列
@param key 属性
@return <#return value description#>
*/
-(NSArray *)sortArray:(NSArray *)array increase:(BOOL)increase byKey:(NSString *)key{

array = [array sortedArrayUsingComparator:
^NSComparisonResult(Car *obj1, Car *obj2) {
NSComparisonResult result;
if ([key isEqualToString:@"price"]) {
CGFloat res1 = [[obj1 valueForKey:key] floatValue];
CGFloat res2 = [[obj2 valueForKey:key] floatValue];
if (increase) {
if (res1 < res2){
result = NSOrderedAscending;
}else {
result = NSOrderedDescending;
}
} else {
if (res1 < res2){
result = NSOrderedDescending;
}else {
result = NSOrderedAscending;
}
}
} else {
result = [[obj1 valueForKey:key] compare:[obj2 valueForKey:key]];
if (!increase) {
if (result == NSOrderedDescending) {
result = NSOrderedAscending;
} else if (result == NSOrderedAscending) {
result = NSOrderedDescending;
}
}
}

return result;
}].mutableCopy;
return array;
}

3、打印结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
NSArray *sortArray;
sortArray = [self sortArray:array increase:YES byKey:@"price"];
NSLog(@"increase by price:");
for (Car *c in sortArray) {
NSLog(@"car:%@ -->price:%lf\n",c.name,c.price);
}

NSLog(@"------分割线------");

sortArray = [self sortArray:array increase:NO byKey:@"price"];
NSLog(@"descend by price:");
for (Car *c in sortArray) {
NSLog(@"car:%@ -->price:%lf\n",c.name,c.price);
}

NSLog(@"------分割线------");

sortArray = [self sortArray:array increase:YES byKey:@"name"];
NSLog(@"increase by name:");
for (Car *c in sortArray) {
NSLog(@"car:%@ -->price:%lf\n",c.name,c.price);
}

NSLog(@"------分割线------");

sortArray = [self sortArray:array increase:NO byKey:@"name"];
NSLog(@"descend by name:");
for (Car *c in sortArray) {
NSLog(@"car:%@ -->price:%lf\n",c.name,c.price);
}

image.png

Warnning: 可能有眼尖的看客发现了,increase by namedescend by name顺序不对,升序的时候Benz不应该在BMW前面吗?降序Benz在BMW后面吗?注意,这里M是大写,而e是小写,大写的ASCII码在小写前面,所以M比e前。

联系方式

邮箱: xiebangyao_1994@163.com

相关账号: