iOS为对象数组排序

作者: 阳光下慵懒的驴 | 来源:发表于2016-05-25 14:24 被阅读1582次

NSSortDescriptor可以根据数组中对象的属性来排序
为排序数组的每个属性创建NSSortDescriptor对象,将所有这些对象放入一个数组中,该数组将会在后面用作参数。使用NSArray类的sortedArrayUsingDescripors:方法并将NSSortDescriptor对象数组作为参数传递过去,会返回一个排好序的数组

创建一个OS X 的Application,选择Command Line Tool,语言为Objective-C

  1. 首先创建一个Person类用于排序
    Person.h
@interface Person : NSObject
@property (nonatomic, copy) NSString *firstName;
@property (nonatomic, copy) NSString *lastName;
@property (nonatomic, assign) NSInteger age;
-(instancetype)initWithFirstName:(NSString *)fName lastName:(NSString *)lName age:(NSInteger) age;
/**
 *  输出状态
 */
-(void)printState;
@end

Person.m

@implementation Person
-(instancetype)initWithFirstName:(NSString *)fName lastName:(NSString *)lName age:(NSInteger)age {
    self = [super init];
    if (self) {
        self.firstName = fName;
        self.lastName = lName;
        self.age = age;
    }
    return self;
}  
-(void)printState {
    NSLog(@"Name is %@ %@ is %ld years old",_firstName, _lastName, _age);
}
@end
  1. 在main.m中排序
#import "Person.h"
int main(int argc, const char * argv[]) {
    Person *p1 = [[Person alloc] initWithFirstName:@"Wenxuan" lastName:@"Huo" age:21];
    Person *p2 = [[Person alloc] initWithFirstName:@"MaHa" lastName:@"B" age:30];
    Person *p3 = [[Person alloc] initWithFirstName:@"MeKe" lastName:@"C" age:30];
    Person *p4 = [[Person alloc] initWithFirstName:@"MaLian" lastName:@"A" age:30];
    Person *p5 = [[Person alloc] initWithFirstName:@"HoHo" lastName:@"A" age:40];
    Person *p6 = [[Person alloc] initWithFirstName:@"Guo" lastName:@"Zhong" age:5000];

    // 包含所有Person的数组
    NSArray *peopleArray = @[p1, p2, p3, p4, p5, p6];

    // 为每个属性创建NSSortDescriptor对象
    NSSortDescriptor * sdFirstName = [NSSortDescriptor sortDescriptorWithKey:@"firstName" ascending:YES];
    NSSortDescriptor * sdLastName = [NSSortDescriptor sortDescriptorWithKey:@"lastName" ascending:YES];
    NSSortDescriptor * sdAge = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:YES];

    // 设置排序优先级,并组成数组。这里优先级最高为age,之后是firstName
    NSArray * sortedArray = [peopleArray sortedArrayUsingDescriptors:@[sdAge, sdLastName, sdFirstName]];
    // 为数组中每个元素执行方法,输出状态
    [sortedArray makeObjectsPerformSelector:@selector(printState)];
     
    return 0;
}

输出结果:

Name is Wenxuan Huo is 21 years old
Name is MaLian A is 30 years old
Name is MaHa B is 30 years old
Name is MeKe C is 30 years old
Name is HoHo A is 40 years old
Name is Guo Zhong is 5000 years old

下面测试另一种排序方式

    // 优先级最高为firstName,之后是age
    NSArray * sortedArray = [peopleArray sortedArrayUsingDescriptors:@[sdLastName, sdAge, sdFirstName]];
    [sortedArray makeObjectsPerformSelector:@selector(printState)];

输出结果:

Name is MaLian A is 30 years old
Name is HoHo A is 40 years old
Name is MaHa B is 30 years old
Name is MeKe C is 30 years old
Name is Wenxuan Huo is 21 years old
Name is Guo Zhong is 5000 years old

Nice

相关文章

  • iOS开发·必会的算法操作:字符串数组排序+模型对象数组排序

    iOS开发·必会的算法操作:字符串数组排序+模型对象数组排序

  • iOS为对象数组排序

    NSSortDescriptor可以根据数组中对象的属性来排序为排序数组的每个属性创建NSSortDescript...

  • iOS 数组排序

    iOS对存放对象的数组排序 1.使用NSComparator进行排序 typedef NSComparisonRe...

  • 排序中文数组 iOS

    冒泡排序方法实现数组排序,数组中元素为中文字符串,方法如下: 参考:ios汉字转拼音iOS解决NSArray、NS...

  • NSArray

    数组中元素排序: res为需要排序对数组, 数组中元素为NEWModel类型对对象、是一个模型。 下面代码的作用就...

  • java 数组和list排序

    数组排序 其中有数组排序和数组对象排序 数组一些数字排序则直接用Arrays.sort()加数组就可以。数组对象则...

  • go数组对象排序

    go数组对象排序 对象数组排序 service 排序前:[{Name:xj16 Sex:男16 Age:16} {...

  • iOS 排序相关

    根据对象里时间排序@"bg_updateTime"为时间字段,array为对象数组

  • js中对象数组按对象属性排序

    数组对象要按属性大小排序怎么办? 数组对象属性排序 我们要如何实现数组中的对象按data大小排序? sort方法 ...

  • JS数组操作

    对象数组排序 标签(空格分隔): JS 一个对象数组 根据id进行正序排序 数组分割

网友评论

    本文标题:iOS为对象数组排序

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