[iOS功能]- iOS数组排序(倒叙 生序 降序)
2019-12-25 本文已影响0人
AlwaysLuckyMa
1.倒序
在ios开发的过程中,经常需要使数组中的数据倒叙排列!比如在tableView显示数据的时候需要使数据倒序排列!那么如何解决数组的倒序排列问题呢?好多开发的小伙伴可能是便利数组的下标来获取,但是这种方法如果数据很大的情况下程序体验度会降低 , 这里介绍一个方法,一句话便可以搞定数组的倒序排序问题!
//
NSMutableArray *temp = [NSMutableArray arrayWithObjects:@"5",@"1",@"4",@"2",nil];
temp = (NSMutableArray *)[[temp reverseObjectEnumerator] allObjects];
2.升序/ 降序
字符串
sortedArrayUsingSelector
//通过自带的compare方法升序排列
NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:@"5",@"1",@"4",@"2",nil];
[array sortUsingSelector:@selector(compare:)];
//通过倒序的方法进行降序排列
NSEnumerator *enumerator = [array reverseObjectEnumerator];
array =[[NSMutableArray alloc]initWithArray: [enumerator allObjects]];
字典
按Key值大小对NSDictionary排序
sortedArrayUsingSelector
NSMutableArray *array = [NSMutableArray arrayWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys:@"Obj0", [NSNumber numberWithInt:0], nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Obj5", [NSNumber numberWithInt:5], nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Obj2", [NSNumber numberWithInt:2], nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Obj3", [NSNumber numberWithInt:3], nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Obj1", [NSNumber numberWithInt:1], nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Obj4", [NSNumber numberWithInt:4], nil], nil];
NSArray *resultArray = [array sortedArrayUsingSelector:@selector(compare:)];
//因为NSDictionary没有compare的排序比较方法,所以需要我们自己写一个
- (NSComparisonResult)compare: (NSDictionary *)otherDictionary{
NSDictionary *tempDictionary = (NSDictionary *)self;
NSNumber *number1 = [[tempDictionary allKeys] objectAtIndex:0];
NSNumber *number2 = [[otherDictionary allKeys] objectAtIndex:0];
NSComparisonResult result = [number1 compare:number2];
return result == NSOrderedDescending; // 升序
// return result == NSOrderedAscending; // 降序
}
sortedArrayUsingComparator
NSMutableArray *array = [NSMutableArray arrayWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys:@"Obj0", [NSNumber numberWithInt:0], nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Obj5", [NSNumber numberWithInt:5], nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Obj2", [NSNumber numberWithInt:2], nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Obj3", [NSNumber numberWithInt:3], nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Obj1", [NSNumber numberWithInt:1], nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Obj4", [NSNumber numberWithInt:4], nil], nil];
// NSArray *resultArray = [array sortedArrayUsingSelector:@selector(compare:)];
NSArray *resultArray = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSNumber *number1 = [[obj1 allKeys] objectAtIndex:0];
NSNumber *number2 = [[obj2 allKeys] objectAtIndex:0];
NSComparisonResult result = [number1 compare:number2];
return result == NSOrderedDescending; // 升序
// return result == NSOrderedAscending; // 降序
}];
数据模型
sortedArrayUsingDescriptors & sortUsingDescriptors
前者带返回值,是NSArray的方法,排好序的数组是返回值中的数组;
后者不带返回值,是NSMutableArray的方法,是对当前数组自己排序
接下来根据一个对象的属性,排列这个对象
.h
@interface Person : NSObject
@property (nonatomic, retain) NSString *name;
@property (nonatomic, assign) NSInteger age;
@end
.m
@implementation Person
@end
排序方法的实现
Person *person1 = [[Person alloc] init];
person1.name = @"Loki";
person1.age = 25;
Person *person2 = [[Person alloc] init];
person2.name = @"Mike";
person2.age = 22;
Person *person3 = [[Person alloc] init];
person3.name = @"Larry";
person3.age = 33;
NSMutableArray *array = [NSMutableArray arrayWithObjects:person1, person2, person3, nil];
//这里类似KVO的读取属性的方法,直接从字符串读取对象属性,注意不要写错
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:YES];
//这个数组保存的是排序好的对象
NSArray *tempArray = [array sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
for(NSInteger i = 0; i < [tempArray count]; i++)
{
NSLog(@"%@--------%d\n", [[tempArray objectAtIndex:i] name], [[tempArray objectAtIndex:i] age]);
}
//下面是可变数组的方法
// [array sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
//
// for(NSInteger i = 0; i < [array count]; i++)
// {
// NSLog(@"%@--------%d\n", [[array objectAtIndex:i] name], [[array objectAtIndex:i] age]);
// }
这里的NSArray中的第一元素表示首先按照这个元素的升序或者降序进行排序,对于有重复项的,再按照第二元素进行排序,依次进行类推
NSSortDescriptor *sortDescriptor1 = [NSSortDescriptorsortDescriptorWithKey:@"age"ascending:YES];
NSSortDescriptor *sortDescriptor2 = [NSSortDescriptorsortDescriptorWithKey:@"name"ascending:YES];
NSArray *tempArray = [array sortedArrayUsingDescriptors:[NSArrayarrayWithObjects:sortDescriptor1, sortDescriptor2, nil]];