简单说明
数组排序比较多,我们讲一种比较常用的,这里涉及到一个知识点:
NSComparisonResult:
typedef enum NSComparisonResult : NSInteger {
NSOrderedAscending = -1L,
NSOrderedSame,
NSOrderedDescending
} NSComparisonResult;
NSOrderedAscending:左边数据比右边小,可理解成增序
NSOrderedSame:相等
NSOrderedDescending:左边数据比右边大,可理解成降序
示例:
NSArray *array = @[@"Benz",@"BMW",@"Ferrari",@"Lamborghini"];
1、普通排序系统自带的升序
NSArray *sortedArray1 = [array sortedArrayUsingSelector:@selector(compare:)];
NSLog(@"普通排序:%@",sortedArray1);
2、逆转数组
NSArray *sortedArray2 = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1,id obj2) {
//逆转
return NSOrderedDescending;
}];
NSLog(@"逆转数组:%@",sortedArray2);
3、不逆转(相当于原数组)
NSArray *sortedArray3 = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1,id obj2) {
//不逆转
return NSOrderedAscending;
}];
NSLog(@"不逆转(原数据):%@",sortedArray3);
4、 升序排列
NSArray *sortedArray4 = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1,id obj2) {
//升序
NSComparisonResult result = [obj1 compare: obj2];
return result;
}];
NSLog(@"升序排列:%@",sortedArray4);
5、降序排列
NSArray *sortedArray5 = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
//降序
NSComparisonResult result = [obj1 compare: obj2];
return -result;
}];
NSLog(@"降序排列:%@",sortedArray5);
进阶:
数组里面存放模型,根据模型的某个属性值来对数组进行重新排序
1)、初始化一些车辆和数组:
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)、排序方法
/**
对数组里存放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)、打印结果
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);
}
Warnning:可能有眼尖的看客发现了,increase by name
和descend by name
顺序不对,升序的时候Benz不应该在BMW前面吗?降序Benz在BMW后面吗?注意,这里M是大写,而e是小写,大写的ASCII码在小写前面,所以M比e前。