刘玉刚--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

这里写图片描述
方法三 利用Block进行排序

 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

结果(按年龄排序1)

这里写图片描述
结果(按人的名字排序2)
这里写图片描述
结果(按car的名字排序3)
这里写图片描述

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/BaiHuaXiu123/article/details/49913415
文章标签: 排序
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

Object-C排序的几种方法

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!