zoukankan      html  css  js  c++  java
  • 数组排序 (数组是有序容器,因此集合中只有数组才能排序。)

    数组排序-NSSortDescriptor · 

    该类能够方便的实现对数组中的对象进行升序或者降序的排序。它可以把元素的某个属性作为key进行升序或降序的排序,每一个NSSortDescriptor 对象就是一个排序条件。 

    创建数组对象

    NSArray *array = @[@“zhonger”, @honghuang”,@“bada”];

    创建排序条件 

    NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"self" ascending:YES]; · · 

    数组根据排序条件进 排序

    NSArray *resultArray = [array sortedArrayUsingDescriptors:@[descriptor]];

    其他数组排序方法 

    不可变数组排序 

      不可变数组排序:(排序结果形成新数组, 原数组不改变) - (NSArray *)sortedArrayUsingSelector: (SEL)comparator; 注:SEL类型的参数comparator:需要传一个返回结果是NSComparisonResult的方法名。 

    例:

    NSArray *array = @[@“zhonger”, @“bada”, @“honghuang”,@“qiuxiang”];

    NSArray *newArray = [array sortedArrayUsingSelector:@selector(compare:)];

    可变数组排序

      可变数组排序:(直接对原数组操作)- (void)sortUsingSelector:(SEL)comparator; 

    注:SEL类型的参数comparator:需要传一个返回结果是NSComparisonResult 的函数 注意:可变数组排序 法与不可变数组

    NSMutableArray *array = [@[@“zhonger”, @“bada”, @“honghuang”,@“qiuxiang”] mutableCopy];

    [array sortUsingSelector:@selector(compare:)];

  • 相关阅读:
    Pandas缺失值处理
    文件读取与存储
    DataFrame运算
    C++11 不抛异常的new operator
    In p = new Fred(), does the Fred memory “leak” if the Fred constructor throws an exception?
    method chaining
    C++中的运算符重载
    Why am I getting an error converting a Foo** → const Foo**?
    The constness of a method should makes sense from outside the object
    Virtual Friend Function
  • 原文地址:https://www.cnblogs.com/d-mm/p/5210217.html
Copyright © 2011-2022 走看看