ios - 在 TableView Controller 中显示之前对 JSON 响应进行排序 - Objective C

标签 ios objective-c iphone json

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/

相关文章:

iphone - 在 IOS 上寻找合适的数据存储

ios - 快速获取所选 View 的索引时出现问题

iphone - 如何在单点触控中无间隙地循环播放 mp3?

android - Cocos2d-x 项目中的宏

ios - Swift 2.0/UITableViewCell - 滚动时无法在顶部位置的单元格中发生操作

ios - 按钮在自定义标注中不起作用

objective-c - calloutAccessoryControlTabbed 委托(delegate)未被调用

objective-c - 如何在 iOS 中使用 AssistiveTouch UI

ios - TextInpout 自动更正在 ios 中显示相反 [react native]

ios - UIPickerview 未出现在正确的位置