Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ios sort array with dictionaries

I have an NSMutablearray, populated by dictionaries [from a JSON request]

i need to organize the array depending to a key in the dictionaries

the dictionary

IdReward = 198;
Name = "Online R d";
RewardImageUrl = "/FileStorimage=1206";
WinRewardPoints = 250;

so the array is composed by different dictionaries with the above form, and I need to organize by maximum to minimum WinRewardPoints

I have seen this answer in SO, but dont understand yet how to adopt it for my case,

thanks a lot!

like image 498
manuelBetancurt Avatar asked Feb 22 '23 03:02

manuelBetancurt


1 Answers

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);
like image 89
Sirji Avatar answered Feb 24 '23 17:02

Sirji