###### 刘玉刚--AI-技术研究院

Liu Yugang -------AI-Technology Research Institute

###### Object-C排序的几种方法

- (NSArray<ObjectType> *)sortedArrayUsingSelector:(SEL)comparator;
NSArray *array = [NSArray arrayWithObjects:@"1",@"9",@"3",@"4",@"2", nil];
array = [array sortedArrayUsingSelector:@selector(compare:)];//系统自带
NSLog(@"%@",array);
注意:也可以重写compare:方法如下所示

**方法二 自定义方法，用选择器（类似于C语言的回调函数）进行排序**
Person.m
@implementation Person
+(Person *)personWithName:(NSString *)name age:(int)age{
Person *p = [[Person alloc]init];
p.age = age;
p.name = name;

return p;
}
-( NSComparisonResult)comparePerson:(Person *)p{
NSComparisonResult re = [[NSNumber numberWithInt:[p age]] compare:[NSNumber numberWithInt:[self age]]];
if (re == NSOrderedSame) {
re = [self.name compare:p.name];
}
return re;
}
-(void) sortArray2{
Person *p1 = [Person personWithName:@"zhangsan" age:12];
Person *p2 = [Person personWithName:@"zhangen" age:12];
Person *p3 = [Person personWithName:@"zhangat" age:12];
Person *p4 = [Person personWithName:@"zhangsrg" age:12];

NSArray *array = [NSArray arrayWithObjects:p1,p2,p3,p4, nil];
NSArray *sortedArray = [array sortedArrayUsingSelector:@selector(comparePerson:)];
NSLog(@"排序后:%@",sortedArray);
}
- (NSString *)description
{
return [NSString stringWithFormat:@"%@,%d", self.name,self.age];
}
@end


 NSArray *array = [NSArray arrayWithObjects:@"1",@"9",@"3",@"4",@"2", nil];

NSComparator res = ^(id obj1, id obj2){

return [obj1 compare:obj2];//直接比较
//            if ([obj1 integerValue] > [obj2 integerValue]) {// 转换成整型值，在进行比较
//                return -1L;
//            }else
//                return 1L;
};
array = [array sortedArrayUsingComparator:res];
NSLog(@"%@",array);

person.m

#import "Person.h"
#import "Car.h"
@implementation Person
+(Person *)personWithName:(NSString *)name age:(int)age{
Person *p = [[Person alloc]init];
p.age = age;
p.name = name;

return p;
}
-(instancetype)initWithName:(NSString *)name age:(int)age car:(Car *)car{
if (self = [super init]) {
self.name = name;
self.age =age;
self.car = car;
}
return self;
}
- (NSString *)description
{
return [NSString stringWithFormat:@"%@,%d,%@", self.name,self.age,self.car];
}
-(void) sortArray2{
Car * c1= [[Car alloc]initWitnName:@"1"];
Car * c2= [[Car alloc]initWitnName:@"2"];
Car * c3= [[Car alloc]initWitnName:@"3"];
Car * c4= [[Car alloc]initWitnName:@"4"];
Person *p1 = [[Person alloc]initWithName:@"z"age:34 car:c1];
Person *p2 = [[Person alloc]initWithName:@"r" age:12 car:c2];
Person *p3 = [[Person alloc]initWithName:@"a" age:17 car:c3];
Person *p4 = [[Person alloc]initWithName:@"c"age:87 car:c4];
NSArray *array = [NSArray arrayWithObjects:p1,p2,p3,p4, nil];
//构建排序描述器
NSSortDescriptor *carNameDesc = [NSSortDescriptor sortDescriptorWithKey:@"car.name" ascending:YES];
// NSSortDescriptor *personNameDesc = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
// NSSortDescriptor *personAgeDesc = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:YES];
// NSArray *descriptorArray = [NSArray arrayWithObjects:personAgeDesc, nil];//结果(按年龄排序1)
// NSArray *descriptorArray = [NSArray arrayWithObjects:personNameDesc, nil];// 结果(按人的名字排序2)
NSArray *descriptorArray = [NSArray arrayWithObjects:carNameDesc, nil];// 结果(按car的名字排序3)

NSArray *sortedArray = [array sortedArrayUsingDescriptors: descriptorArray];
NSLog(@"%@",sortedArray);
}

@end

Car.m

#import "Car.h"

@implementation Car
-(instancetype)initWitnName:(NSString *)name{
if (self = [super init]) {
self.name = name;
}
return self;
}
- (NSString *)description
{
return [NSString stringWithFormat:@"%@", self.name];
}
@end

#### NSSortDescriptor的使用

2013-07-24 22:17:47

#### object-c 多参数方法

2015-06-10 17:55:36

#### Object-C中的类方法和实例方法区别

2014-03-21 10:40:40

#### Object-C 方法

2016-04-17 16:01:26

#### Object-C，对象和方法

2015-12-03 21:05:26

#### Object-C调用类内方法

2018-03-18 23:02:45

#### 第一篇 Object-C快速入门

2015-01-29 16:27:57

#### 给object数组进行排序（排序条件是每个元素对象的属性个数）

2015-09-09 16:57:57

#### Object-C,NSArraySortTest，数组排序3种方式

2015-12-02 20:48:20

#### object-c方法

2016-05-09 18:01:18

Object-C排序的几种方法