Rewards = (
{
"Restaurant_ID" = 34;
"Rewarded_Points" = 40;
"Rewarded_Time" = "2015-11-23 09:16:00";
},
{
"Restaurant_ID" = 40;
"Rewarded_Points" = 60;
"Rewarded_Time" = "2015-11-24 12:22:56";
},
{
"Restaurant_ID" = 34;
"Rewarded_Points" = 40;
"Rewarded_Time" = "2015-11-24 12:22:56";
}
);
我正在检索以上对变量的响应并使用 for 循环在 TableViewController 中显示
for(int i=0;i<rewards.count;i++){
NSDictionary * rewardsObj = [rewards objectAtIndex:i];
restId = [rewardsObj objectForKey:@"Restaurant_ID"];
rewardedPoints = [rewardsObj objectForKey:@"Rewarded_Points"];
}
[self.tableView reloadData];
当前输出一次又一次地显示相同的餐厅 ID。我想如果有匹配的 ID,那么这些点需要计算总和。
34 - 总分:40
40 - 总分:60
34 - 总分:40
//再次
但我想像下面这样排序
34 - 总分:80
//40+40
40 - 总分:60
最佳答案
为您的概念使用 NSSortDescriptor
,
步骤-1
最初根据排序选择键,对于我们取的ex Rewarded_Points
,并按升序
顺序排序。
NSSortDescriptor * Rewarded_Points =
[[NSSortDescriptor alloc] initWithKey:@"Rewarded_Points"
ascending:YES];
NSArray *descriptors = [NSArray arrayWithObjects:Rewarded_Points, nil];
NSArray *sortedArrayOfDictionaries = [Rewards sortedArrayUsingDescriptors:descriptors]; // Rewards -> array of dictionary name
NSLog(@"final Value : %@", sortedArrayOfDictionaries);
额外Tutorial
关于ios - 在 TableView Controller 中显示之前对 JSON 响应进行排序 - Objective C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33887537/