项目中需要根据type的值来分数组
NSArray *ary = [NSArray yy_modelArrayWithClass:ActionModel.class json:data];
//得到数组中的type并去掉重复的
NSMutableDictionary *typesDic = NSMutableDictionary.new;
for (int i=0; i<ary.count; i++) {
ActionModel *model = ary[i];
[typesDic setObject:@(model.type) forKey:@(model.type)];
}
// 对数组进行排序
NSArray *result = [typesDic.allValues sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
// NSLog(@"%@~%@",obj1,obj2); // 3~4 2~1 3~1 3~2
return [obj1 compare:obj2]; // 升序
// return [obj2 compare:obj1]; // 降序
}];
//根据数组中的type分数组
NSMutableArray *redDataAry = NSMutableArray.new;
for (int i=0; i<typesDic.allValues.count; i++) {
NSNumber *type = typesDic.allValues[i];
NSMutableArray *typeAry = NSMutableArray.new;
for (ActionModel *model in ary ) {
if (model.type == type.intValue) {
[typeAry addObject:model];
}
}
[redDataAry addObject:typeAry];
}
``
网友评论