您的位置:首页 > 移动开发 > IOS开发

ios开发小知识(对象数组排序)

2017-12-25 11:55 253 查看
如何对包含自定义元素类型的NSMutableArray排序

//JSDataModel.h文件

@interface JSDataModel:NSObject

@property(nonatomic,copy) NSString *userName;

@property(nonatomic,copy) NSString *sortNum;

@end

//初始化数据

NSMutableArray *tempArray = [NSMutableArray array];

for (NSUInteger i =5; i>0; i--) {

JSDataModel *model = [[JSDataModel alloc]init];

model.userName = @"userName";

model.sortNum = [@(i) stringValue];

[tempArray addObject:model];

}

//对对象数组中的所有元素排除

NSSortDescriptor *sorter = [[NSSortDescriptor alloc]initWithKey:@"sortNum" ascending:YES];

NSArray *temp = [tempArray sortedArrayUsingDescriptors:@[sorter]];//排序后的数组

NSLog(@"%@",temp);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: