zoukankan      html  css  js  c++  java
  • iOS开发基础之排序

    Objective-C 有排序的API,省了我们很多事。

    主要有以下3种方法。

    • NSComparator      
    NSArray *unsortedArray = @[@5,@3,@8,@1,@7];
    
    NSArray *sortedArray = [unsortedArray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        return [obj1 compare:obj2];
    }];
    • NSDescriptor
    NSArray *unsortedArray = @[@5,@3,@8,@1,@7];
    
    NSSortDescriptor *aDesc = [[NSSortDescriptor alloc] initWithKey:@"integerValue" ascending:YES];
    NSArray *sortedArray2 = [unsortedArray sortedArrayUsingDescriptors:@[aDesc]];
    
    • 自定义selector

    注意到方法一:

    [obj1 compare:obj2]; 是NSNumber实现的compare方法,对于自己定义的Model,可以实现自己的compare方法。
    - (NSComparisonResult)compare:(Person *)otherPerson {
    return [self.dateOfBirth compare:otherPerson.dateOfBirth];
    }
    

     如这段代码,是按照Person的生日排序。

  • 相关阅读:
    SQL中的union
    SQL的类型转换
    Keytool生成证书
    Openssl生成证书
    Python示例-Json Parse
    Python示例-TCP Port Scan
    Python套接字
    TCP端口扫描
    Linux环境变量
    Python示例-Logging
  • 原文地址:https://www.cnblogs.com/bitnpc/p/4613861.html
Copyright © 2011-2022 走看看