闲来无事,看了看OC的对象数组的排序,简单地展示一下常用的三种方式:
首先,看一下自定义对象的属性和方法:
@interface People : NSObject
@property (nonatomic,strong) NSString *name;
@property (nonatomic,assign) NSInteger age;
@property (nonatomic,strong) NSString *gender;
@property (nonatomic,strong) NSString *address;
-(NSComparisonResult)comparePeople:(People *)peo;
@end
@implementation People
-(NSComparisonResult)comparePeople:(People *)peo{
return [self.name compare:peo.name];
}
@end
下面,我们来生成一些数据:
NSMutableArray *people_arr = [NSMutableArray new];
for (int i = 0; i < 10; i++) {
People *people = [People new];
people.name = @[@"jack",@"jester",@"jester",@"joce",@"joke",@"java",@"amy",@"jason",@"mike",@"jwt"];
people.age = 15+i;
people.gender = i%2? @"男":@"女";
people.address = @[@"郑州",@"上海",@"济南",@"北京",@"杭州"][arc4random()%5];
[people_arr addObject:people];
}
NSLog(@"%@", people_arr);
第一种方式: 利用 NSSortDescriptor 对对象数组,按照某一属性或某些属性的升序降序排列
NSSortDescriptor *des1 = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
NSSortDescriptor *des2 = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:NO];
[people_arr sortUsingDescriptors:@[des1, des2]];
这种方式,利用多个NSSortDescriptor对象的组合使用,进行排序,而且组合顺序不同,得到的结果应该也会有所差别。这里没有进行测试,想探索的可以自己去试一试
第二种方式:利用 block回调 对对象数组,按照某一属性或某些属性的升序降序排列
[people_arr sortUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
People *p1 = (People *)obj1;
People *p2 = (People *)obj2;
return [p1.name compare:p2.name];
}];
第三种方式:利用自定义比较方法 对对象数组,按照某一属性或某些属性的升序降序排列
[people_arr sortUsingSelector:@selector(comparePeople:)];
这样,三种方式就简单实现了,是不是很简单
首先,看一下自定义对象的属性和方法:
@interface People : NSObject
@property (nonatomic,strong) NSString *name;
@property (nonatomic,assign) NSInteger age;
@property (nonatomic,strong) NSString *gender;
@property (nonatomic,strong) NSString *address;
-(NSComparisonResult)comparePeople:(People *)peo;
@end
@implementation People
-(NSComparisonResult)comparePeople:(People *)peo{
return [self.name compare:peo.name];
}
@end
下面,我们来生成一些数据:
NSMutableArray *people_arr = [NSMutableArray new];
for (int i = 0; i < 10; i++) {
People *people = [People new];
people.name = @[@"jack",@"jester",@"jester",@"joce",@"joke",@"java",@"amy",@"jason",@"mike",@"jwt"];
people.age = 15+i;
people.gender = i%2? @"男":@"女";
people.address = @[@"郑州",@"上海",@"济南",@"北京",@"杭州"][arc4random()%5];
[people_arr addObject:people];
}
NSLog(@"%@", people_arr);
第一种方式: 利用 NSSortDescriptor 对对象数组,按照某一属性或某些属性的升序降序排列
NSSortDescriptor *des1 = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
NSSortDescriptor *des2 = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:NO];
[people_arr sortUsingDescriptors:@[des1, des2]];
这种方式,利用多个NSSortDescriptor对象的组合使用,进行排序,而且组合顺序不同,得到的结果应该也会有所差别。这里没有进行测试,想探索的可以自己去试一试
第二种方式:利用 block回调 对对象数组,按照某一属性或某些属性的升序降序排列
[people_arr sortUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
People *p1 = (People *)obj1;
People *p2 = (People *)obj2;
return [p1.name compare:p2.name];
}];
第三种方式:利用自定义比较方法 对对象数组,按照某一属性或某些属性的升序降序排列
[people_arr sortUsingSelector:@selector(comparePeople:)];
这样,三种方式就简单实现了,是不是很简单