Object-C排序的几种方法

标签: 排序
576人阅读 评论(0) 收藏 举报

方法一
利用系统自带的排序方法:

- (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)
这里写图片描述

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
      个人说明

      以梦为马 不负韶华!


      - The king of Kings.


      人生伟业的建立,不在能知,乃在能行。


      青春不老,奋斗不止。
    个人资料
    • 访问:946920次
    • 积分:12641
    • 等级:
    • 排名:第1142名
    • 原创:330篇
    • 转载:66篇
    • 译文:15篇
    • 评论:410条
      技术地址
      微信公众号
      微信号
      QQ号
    博客专栏