美文网首页
iOS数组排序-根据模型的属性排序

iOS数组排序-根据模型的属性排序

作者: 瞬csr | 来源:发表于2019-04-04 13:44 被阅读0次

1.如下model

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface CSRAttentionModel : NSObject

@property(nonatomic) NSInteger createTime;

@end

NS_ASSUME_NONNULL_END

2.数组

  NSMutableArray *array = [NSMutableArray arrayWithCapacity:0];
  
  CSRAttentionModel* attentionModel = [[CSRAttentionModel alloc] init];
  attentionModel.createTime = 10;
  [array addObject:attentionModel];
  
  CSRAttentionModel* attentionModel2 = [[CSRAttentionModel alloc] init];
  attentionModel2.createTime = 10;
  [array addObject:attentionModel2];
  
  CSRAttentionModel* attentionModel3 = [[CSRAttentionModel alloc] init];
  attentionModel3.createTime = 8;
  [array addObject:attentionModel3];
  
  CSRAttentionModel* attentionModel4 = [[CSRAttentionModel alloc] init];
  attentionModel4.createTime = 4;
  [array addObject:attentionModel4];
  
  CSRAttentionModel* attentionModel5 = [[CSRAttentionModel alloc] init];
  attentionModel5.createTime = 17;
  [array addObject:attentionModel5];

3.根据model的属性createTime降序排序数组

NSSortDescriptor* sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"createTime" ascending:NO];
NSArray* sortPackageResListArr = [array sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
NSLog(@"%@",sortPackageResListArr);

5.排序前后对比


排序前.png 排序后.png
6.扩展:
若想升序排序,只需要将ascending:的值传YES即可。
若还有另一个属性state,想在此前排序的基础上,在增加一条规则,如:优先按createTime降序,其次按state升序排序,则可如下:
NSSortDescriptor* sortDescriptor1 = [NSSortDescriptor sortDescriptorWithKey:@"createTime" ascending:NO];
NSSortDescriptor* sortDescriptor2 = [NSSortDescriptor sortDescriptorWithKey:@"state" ascending:YES];
NSArray* sortPackageResListArr = [array sortedArrayUsingDescriptors:@[sortDescriptor1,sortDescriptor2]];

相关文章

网友评论

      本文标题:iOS数组排序-根据模型的属性排序

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