数组通过time拆分新的数组(包含多个时间段的数组)
初始json数据如下:
{
info_list : [
{
id : 110,
time : "2016-6-20",
name : "张三"
},
{
id : 111,
time : "2016-6-20",
name : "李四"
},
{
id : 112,
time : "2016-6-21",
name : "王五"
},
{
id : 113,
time : "2016-6-22",
name : "赵六"
}
]
}
改造后如下:
{
info_list : [
[
{
id : 110,
time : "2016-6-20",
name : "张三"
},
{
id : 111,
time : "2016-6-20",
name : "李四"
}
],
[
{
id : 112,
time : "2016-6-21",
name : "王五"
}
],
[
{
id : 113,
time : "2016-6-22",
name : "赵六"
}
]
]
}
也就是数组中包含数组,多加了一层.
- (NSMutableArray *)arraySplitSubArrays:(NSArray *)array {
// 数组去重,根据数组元素对象中time字段去重
NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithCapacity:0];
for(ShoppingDetailsRecordModel *obj in array)
{
[dic setValue:obj forKey:[Tool timeStampWithtimeStr:obj.time dateFormat:@"yyyy-MM-dd"]];
}
NSMutableArray *tempArr = [NSMutableArray array];
for (NSString *dictKey in dic) {
[tempArr addObject:dictKey];
}
NSArray *sortedArray = [tempArr sortedArrayUsingSelector:@selector(compare:)];
NSLog(@"排序后:%@",sortedArray);
// 字典重不会有重复值,allKeys返回的是无序的数组
NSLog(@"去重后字典:%@",[dic allKeys]);
NSMutableArray *temps = [NSMutableArray array];
for (NSString *dictKey in sortedArray) {
NSMutableArray *subTemps = [NSMutableArray array];
for (ShoppingDetailsRecordModel *obj in array) {
if ([dictKey isEqualToString:[Tool timeStampWithtimeStr:obj.time dateFormat:@"yyyy-MM-dd"]]) {
[subTemps addObject:obj];
}
}
[temps addObject:subTemps];
}
// 排序后,元素倒序的,逆向遍历
NSEnumerator *enumerator = [temps reverseObjectEnumerator];
temps = (NSMutableArray*)[enumerator allObjects];
ZLLog(@"temps:%@",temps);
return temps;
}