NSSortDescriptor排序

作者: 徊家喂猪 | 来源:发表于2017-01-23 23:29 被阅读54次

    在OC中,使用NSSortDescriptor可以实现按对象的属性进行排序。例如,我们有个User对象数组,想根据对象的年龄或者姓名拼音的首字母排序,就可以使用NSSortDescriptor。

    首先,先简单的看一下这个类的效果。

    NSArray *array = @[@"c", @"b", @"a", @"e", @"d"];
    
    //创建一个排序条件,也就是一个NSSortDescriptor对象
    //第一个参数为数组中对象要按照什么属性来排序(如自身、姓名,年龄等)
    //第二个参数为指定排序方式是升序还是降序
    //ascending  排序的意思,默认为YES 升序
    NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"self" ascending:NO];
    
    NSArray *newArray = [array sortedArrayUsingDescriptors:@[sort]];
    
    NSLog(@"%@", newArray);
    
    排序效果
    sortedArrayUsingDescriptors: 这个方法的参数是一个数组,可以将多个排序条件放到数组中,按先后顺序,先加入的优先级高。

    新建一个User类,User.h代码如下

      /** 姓名 */
      @property (nonatomic, copy) NSString *name;
    
      /** 年龄 */
      @property (nonatomic, assign) NSInteger age;
    
      - (instancetype) initWithName:(NSString *)name Age:(NSInteger)age;
    

    User.m代码如下

    - (instancetype) initWithName:(NSString *)name Age:(NSInteger)age
    {
        self = [super init];
        if (self) {
            self.name = name;
            self.age  = age;
        }
        return self;
    }
    

    接下来我们看一下在ViewController中的具体实现:

    // 创建5个user对象
    User *user1 = [[User alloc] initWithName:@"MM" Age:8];
    User *user2 = [[User alloc] initWithName:@"MM" Age:3];
    User *user3 = [[User alloc] initWithName:@"AA" Age:2];
    User *user4 = [[User alloc] initWithName:@"SS" Age:7];
    User *user5 = [[User alloc] initWithName:@"GG" Age:5];
    
    // 将创建好的对象放入数组中,等待排序
    NSArray *userArr = @[user1, user2, user3, user4, user5];
    
    // 创建两个排序条件,分别按照姓名首字母排序和年龄排序
    NSSortDescriptor *des1 = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    
    NSSortDescriptor *des2 = [[NSSortDescriptor alloc] initWithKey:@"age" ascending:NO];
    
    // 将两个排序条件添加到数组中
    NSArray *newUserArr = [userArr sortedArrayUsingDescriptors:@[des1, des2]];
    
    // 循环输出一下得到的新数组
    for (User *user in newUserArr) {
        
        NSLog(@"姓名:%@ -- 年龄:%@", user.name, user.age);
        
    }
    
    排序效果
    sortedArrayUsingSelector:这个方法的参数是一个方法,我们可以自定义自己的排序规则

    修改User类,在User.h中增加声明一个方法,代码如下:

    /** 姓名 */
    @property (nonatomic, copy) NSString *name;
    
    /** 年龄 */
    @property (nonatomic, assign) NSInteger age;
    
    - (instancetype) initWithName:(NSString *)name Age:(NSInteger)age;
    
    // 增加声明的方法,用于自定义排序方法
    - (NSComparisonResult)compareByAge:(User *)otherUser;
    

    在User.m中实现该方法:

    - (instancetype) initWithName:(NSString *)name Age:(NSInteger)age
    {
        self = [super init];
        if (self) {
            self.name = name;
            self.age  = age;
        }
        return self;
    }
    
    - (NSComparisonResult)compareByAge:(User *)otherUser
    {
        // _age 为 User的属性
        if (_age > [otherUser age]) {
        
            return NSOrderedAscending;
        } else if(_age == [otherUser age]) {
        
            return NSOrderedSame;
        } else{
        
            return NSOrderedDescending;
        }
    }
    

    在ViewController中的实现也修改一下:

    // 创建5个user对象
    User *user1 = [[User alloc] initWithName:@"MM" Age:8];
    User *user2 = [[User alloc] initWithName:@"MM" Age:3];
    User *user3 = [[User alloc] initWithName:@"AA" Age:2];
    User *user4 = [[User alloc] initWithName:@"SS" Age:7];
    User *user5 = [[User alloc] initWithName:@"GG" Age:5];
    
    // 将创建好的对象放入数组中,等待排序
    NSArray *userArr = @[user1, user2, user3, user4, user5];
    
    // 不需要创建排序条件,而是直接调用User类中写好的排序方法
    NSArray *newUserArr = [userArr sortedArrayUsingSelector:@selector(compareByAge:)];
    
    for (User *user in newUserArr) {
        
        NSLog(@"姓名:%@ -- 年龄:%ld", user.name, user.age);
        
    }
    

    看一下执行结果吧!

    自定义排序方法结果

    相关文章

      网友评论

        本文标题:NSSortDescriptor排序

        本文链接:https://www.haomeiwen.com/subject/fdbabttx.html