Viraj Padsala
Viraj Padsala

Reputation: 1398

How to sort NSMutableDictionary with dynamic keys in ios?

I am getting following dictionary as JSON response

Lease =     {
    "KINDERSLEY KERROBERT" =
    (
     {
         "lease_code" = 37;
     },
     {
         "lease_code" = 38;
     }
     );
    LLOYDMINSTER =
    (
     {
         "lease_code" = 68;
     },
     {
         "lease_code" = 69;
     }
     );
    "SOUTHEASTERN SASKATCHEWAN" =
    (
     {
         "lease_code" = 1;
     },
     {
         "lease_code" = 2;
     }
     );
    "SWIFT CURRENT" =
    (
     {
         "lease_code" = 32;
     },
     {
         "lease_code" = 33;
     }
     );
    };

I would like to sort it as follow:

 Lease =     {
"SOUTHEASTERN SASKATCHEWAN" =
(
 {
     "lease_code" = 1;
 },
 {
     "lease_code" = 2;
 }
 );
"SWIFT CURRENT" =
(
 {
     "lease_code" = 32;
 },
 {
     "lease_code" = 33;
 }
 );
"KINDERSLEY KERROBERT" =
(
 {
     "lease_code" = 37;
 },
 {
     "lease_code" = 38;
 }
 );
LLOYDMINSTER =
(
 {
     "lease_code" = 68;
 },
 {
     "lease_code" = 69;
 }
 );
};

I wrote below code to sort dictionary:

- (NSArray *)sortKeysByIntValue:(NSDictionary *)dictionary {

    NSArray *sortedKeys = [dictionary keysSortedByValueUsingComparator:^NSComparisonResult(NSArray *obj1, NSArray *obj2) {
        NSString* key1 = [[obj1 objectAtIndex:0] objectForKey:@"lease_code"];
        NSString* key2 = [[obj2 objectAtIndex:0] objectForKey:@"lease_code"];
        return [key1 compare:key2];
    }];
    return sortedKeys;
}

It gives me sortedKeys that I want, But after rebuilding NSMutableDictionary using below code:

NSArray *sortedKeys = [self sortKeysByIntValue:dictionary];
NSMutableDictionary *sortedDictionary = [[NSMutableDictionary alloc] init];

for (NSString *key in sortedKeys) {
    [sortedDictionary setObject:dictionary[key] forKey:key];
}

It again unsorted as shown in response.

please help me out

Upvotes: 0

Views: 643

Answers (2)

orkoden
orkoden

Reputation: 20026

Dictionaries are always unsorted. They are used as a key value storage to look up data. You should keep the order of your data separate from the dictionary. You need the dictionary you already have AND a sorted array of the keys.

@property NSDictionary *dataDict;
@property NSArray *sortedKeys;

self.sortedKeys = [self sortKeysByIntValue:self.dataDict];

In a UITableViewDataSource method, you would first consult your array with the index, get the key, and then retrieve the data from the dictionary.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger row = [indexPath row];
    NSString *key = [self.sortedkeys objectAtIndex:row];
    NSObject *dataObject = [self.dataDict valueForKey:key];

Upvotes: 3

Andrey Chernukha
Andrey Chernukha

Reputation: 21808

NSArray *sortedKeys = [self sortKeysByIntValue:dictionary];
NSMutableArray* values = [NSMutableArray array];

for (NSString* key in sortedKeys)
{
   [values addObject:dictionary[key]];
} 

Now you have both your keys and values sorted. This is exactly what you need

Upvotes: 1

Related Questions