数组排序之 NSOrderedAscending 和 NSOrderedDescending
看源码
/*
These constants are used to indicate how items in a request are ordered, from the first one given in a method invocation or function call to the last (that is, left to right in code).
Given the function:
NSComparisonResult f(int a, int b)
If:
a < b then return NSOrderedAscending. The left operand is smaller than the right operand.
a > b then return NSOrderedDescending. The left operand is greater than the right operand.
a == b then return NSOrderedSame. The operands are equal.
*/
// 注意!! 这里的升序降序指的是 按照 index 升序降序
typedef NS_CLOSED_ENUM(NSInteger, NSComparisonResult) {
NSOrderedAscending = -1L,// 升序排列
NSOrderedSame,// 相等
NSOrderedDescending// 降序排列
};
typedef NSComparisonResult (^NSComparator)(id obj1, id obj2);
探索
[self testSortedArrayFuntoinWithType:NSOrderedAscending];
[self testSortedArrayFuntoinWithType:NSOrderedSame];
[self testSortedArrayFuntoinWithType:NSOrderedDescending];
- (void)testSortedArrayFuntoinWithType:(NSComparisonResult)type {
NSLog(@"------>>>>>>>>");
NSArray *array = @[@1,@8,@4,@5,@2,@3];
NSMutableArray *mutableArray1 = [NSMutableArray arrayWithArray:array];
NSMutableArray *mutableArray2 = [NSMutableArray arrayWithArray:array];
NSMutableArray *mutableArray3 = [NSMutableArray arrayWithArray:array];
NSArray *stringArray = @[@"name",@"age",@"sex",@"height",@"weight",@"namy"];
NSArray *mixArray = @[@"name",@8,@1,@"age",@"sex",@5,@"height",@"2",@"3"];
NSArray *numArray = @[@"1",@"8",@"4",@"5",@"2",@"3"];
NSArray *mixArray1 = [mixArray sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
return type;
}];
NSArray *numArray1 = [numArray sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
return type;
}];
NSLog(@"字符传+数值 混合顺序:mixArray1%@\n--String数值顺序:numArray1%@",mixArray1,numArray1);
NSArray *sourtArray1 = [stringArray sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
// 将数组中的字符串升序排列
return type;
}];
NSLog(@"字符串顺序:sourArray1%@",sourtArray1);
[mutableArray1 sortUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
// 将数组中的对象升序排列
return type;
}];
NSLog(@"NSNumber 顺序:mutableArray1%@",mutableArray1);
NSLog(@"------>>>>>>>>");
}
NSOrderedAscending 和 NSOrderedSame
NSOrderedAscending
和 NSOrderedSame
保持数组的排序不变.
NSOrderedDescending
当返回值为NSOrderedDescending
时,会按照index降序排列,或者说是将数组逆序。
综上所述,
NSComparisonResult
类型的枚举值 会根据index进行排序,并不是根据 value进行排序。
那我们如果想根据 value 进行 升序 或者 降序排列 该如何操作?
针对 value 排序
// 升序
[mutableArray3 sortUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
if ([obj1 compare:obj2] == NSOrderedAscending) {
return NSOrderedAscending;
}else {
return NSOrderedDescending;
}
}];
NSLog(@"NSNumber 数值升序:mutableArray3%@",mutableArray3);
// 降序
[mutableArray3 sortUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
if ([obj1 compare:obj2] != NSOrderedAscending) {
return NSOrderedAscending;
}else {
return NSOrderedDescending;
}
}];
NSLog(@"NSNumber 数值降序:mutableArray3%@",mutableArray3);
// 针对字符串 升序排列
NSArray *sourtArray2 = [stringArray sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
return [obj1 compare:obj2];
}];
NSLog(@"字符串排序 :sourArray2%@",sourtArray2);
打印结果
NSNumber 数值升序:mutableArray3(
1,
2,
3,
4,
5,
8
)
NSNumber 数值降序:mutableArray3(
8,
5,
4,
3,
2,
1
)
字符串排序 :sourArray2(
age,
height,
name,
namy,
sex,
weight
)
避坑
需要注意的是,当数组内的数据元素是混合类型(并非单一一种数据类型)时,这里会出现crash!
NSArray *mixArray = @[@3,@"name",@8,@1,@"age",@"sex",@5,@"height",@"2",@"3"];
NSArray *sourtArray2 = [mixArray sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
return [obj1 compare:obj2];
}];
// crash 打印结果
[__NSCFConstantString objCType]: unrecognized selector sent to instance 0x104b98268
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString objCType]: unrecognized selector sent to instance 0x104b98268'
*** First throw call stack:
(0x18ae9b298 0x19ebf5480 0x18adaa2a8 0x18ae9d8f4 0x18ae9f89c 0x18c1dbd28 0x104b90a68 0x18ae2d978 0x18ae2da08 0x18ae2da08 0x18ae2d4e8 0x18ad84414 0x104b905fc 0x104b9027c 0x18d074e80 0x18d079438 0x18cfc0f3c 0x18cfc1248 0x18cfc210c 0x18cfc3490 0x18cfa64c4 0x18dc3e6d4 0x18e0b4424 0x18e0babac 0x18e0c616c 0x18e00e578 0x18e0392c8 0x18d75c6c8 0x18ae1a650 0x18ae198e4 0x18ae14074 0x18ae13818 0x1a1519570 0x18d73f0e8 0x18d744664 0x104b929e8 0x18aaf2140)
libc++abi: terminating with uncaught exception of type NSException
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString objCType]: unrecognized selector sent to instance 0x104b98268'
terminating with uncaught exception of type NSException
所以我们在使用该方法进行数组元素排序时,记得判断下数据类型。