美文网首页iOS开发
iOS-将数组内的模型对象排序

iOS-将数组内的模型对象排序

作者: 拎着猫走的鱼 | 来源:发表于2019-06-10 13:35 被阅读0次
  • 创建两个Person对象,并把它们放入数组,按年龄升序排序
Person *person1 = [[Person  alloc] init];
Person *person2 = [[Person  alloc] init];

person1.age = @"8";
person2.age = @"5";

NSMutableArray *arr = [NSMutableArray arrayWithObjects: person1, person2, nil];

NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:YES];

NSArray *newArr = [topArr sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];

此时newArr中,是以person2, person1的顺序存储。

相关文章

网友评论

    本文标题:iOS-将数组内的模型对象排序

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