我有一个NSMutablearray,由字典填充(来自JSON请求)
我需要根据字典中的键来组织数组
词典
IdReward = 198;
Name = "Online R d";
RewardImageUrl = "/FileStorimage=1206";
WinRewardPoints = 250;
因此数组由具有以上形式的不同字典组成,我需要按最大到最小个
WinRewardPoints
进行组织我在SO中看过this answer,但还不了解如何针对我的情况采用它,
非常感谢!
最佳答案
IdReward = 198;
Name = "Online R d";
RewardImageUrl = "/FileStorimage=1206";
WinRewardPoints = 250;
NSMutableArray *arr = [[NSMutableArray alloc]init];
for (int i=0; i<[dict count]; i++) {
[arr addObject:[dict valueForKey:@"WinRewardPoints"]];
}
NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:nil ascending:YES] autorelease];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
arr = [[arr sortedArrayUsingDescriptors:sortDescriptors] copy];
NSMutableArray *final_arr = [[NSMutableArray alloc]init];
for(NSString* str in arr)<p>
{
for(int i=0 ; i<[dict count]; i++)
{
<t>if ([str isEqualToString:[[dict valueForKey:@"WinRewardPoints"]objectAtIndex:i]])
{
[final_arr addObject:[dict objectAtIndex:i]];
}
}
}
NSLog(@"%@",final_arr);