- 首先,我的數(shù)據(jù)結(jié)構(gòu)是這樣的
"list": [
{
"d_id": "603912913ce90e276a84",
"a_id": 9063,
"a_t": 2024-01-16
},
{
"d_id": "19404a13c8478c1e4151",
"a_id": 9002,
"a_t": 2024-01-17
},
{
"d_id": "697189647cb94cb07f91",
"a_id": 7,
"a_t": 2024-01-16
}
]
需求:把"a_t"對應(yīng)的值相同的字典對象組成一個數(shù)組弯囊,并對list數(shù)組中各字典依據(jù)key值"a_t"進行排序。
- 做法如下:
for (NSDictionary *tempDic in [infoDic objectForKey:@"list"]) {
L_JingGaoModel *model = [L_JingGaoModel modelWithDictionary:tempDic];
[tempArray addObject:model];
[self.dataArray addObject:model];
NSInteger ttttt = [model.ts integerValue];
NSString *dateStr = [self timestampSwitchDateString:ttttt andFormatter:@"YYYY-MM-dd"];
if (!self.dataDic[dateStr]) {
self.dataDic[dateStr] = [NSMutableArray array];
}
[self.dataDic[dateStr] addObject:model];
}
MyLog(@"ts---- ---- %@",self.dataDic);
/**
對字典(Key-Value)排序 區(qū)分大小寫
@param dict 要排序的字典
*/
- (void)sortedDictionary:(NSMutableDictionary *)dict{
//將所有的key放進數(shù)組
NSArray *allKeyArray = [dict allKeys];
//序列化器對數(shù)組進行排序的block 返回值為排序后的數(shù)組
NSArray *afterSortKeyArray = [allKeyArray sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
NSComparisonResult resuest = [obj2 compare:obj1];//降序
return resuest;
}];
//NSLog(@"afterSortKeyArray:%@",afterSortKeyArray);
self.keyArr = [[NSMutableArray alloc] initWithArray:afterSortKeyArray];
//通過排列的key值獲取value
NSMutableArray *valueArray = [NSMutableArray array];
for (NSString *sortsing in afterSortKeyArray) {
NSString *valueString = [dict objectForKey:sortsing];
[valueArray addObject:valueString];
}
//NSLog(@"valueArray:%@",valueArray);
self.valueArr = [valueArray mutableCopy];
}
- 結(jié)果:如下所示