0

Hi This is what am getting from server

{
        1 =         {

            "display_name" = "One";
            id = 1;
     };
      2 =         {

            "display_name" = "Two";
            id = 2;
     };
      13 =         {

            "display_name" = "abc";
            id = 13;
     };
      15 =         {

            "display_name" = "aaa";
            id = 15;
     };
     4 =         {

            "display_name" = "ffd";
            id = 4;
     };
      3 =         {

            "display_name" = "abdfdfc";
            id = 3;
     };
      5 =         {

            "display_name" = "aasdfsdfa";
            id = 5;
     };
}

i need to sort this based on "id" this is what am looking as output

Expecting output

{
        1 =         {

            "display_name" = "One";
            id = 1;
     };
      2 =         {

            "display_name" = "Two";
            id = 2;
     };
      3 =         {

            "display_name" = "abdfdfc";
            id = 3;
     };
      4 =         {

            "display_name" = "ffd";
            id = 4;
     };
     5 =         {

            "display_name" = "aasdfsdfa";
            id = 5;
     };
      13 =         {

            "display_name" = "abc";
            id = 13;
     };
      15 =         {

            "display_name" = "aaa";
            id = 15;
     };
}

This code i have tried and its not working

 //vehiclesDictionary   real dictionary
        NSMutableArray *sortedKeys=[[NSMutableArray alloc]init];
         for(NSString *item in [vehiclesDictionary allKeys]){
             [sortedKeys addObject:[NSNumber numberWithInt:[item intValue]]];
         }


         NSArray *sortedKeysArray = [sortedKeys sortedArrayUsingSelector: @selector(compare:)];
         NSLog(@"%@",sortedKeysArray);

         NSMutableDictionary *sortedValues = [[NSMutableDictionary alloc] init];
         for (NSString *key in sortedKeysArray) {
             [sortedValues setValue:[vehiclesDictionary valueForKey:[NSString stringWithFormat:@"%@",key]] forKey:key];
         }

         NSLog(@"%@",sortedValues);

Pls help me

Bangalore
  • 1,572
  • 4
  • 20
  • 50
  • pls explain not working. – Teja Nandamuri Apr 08 '16 at 15:16
  • Possible duplicate of [Getting NSDictionary keys sorted by their respective values](http://stackoverflow.com/questions/9708742/getting-nsdictionary-keys-sorted-by-their-respective-values) – Teja Nandamuri Apr 08 '16 at 15:19
  • 3
    Basically a dictionary is an unordered collection type by definition and cannot be sorted. – vadian Apr 08 '16 at 15:24
  • 2
    NSDictionary is not ordered (by key). It does not matter if you sort the key's and put everything in sorted order into another NSDictionary. If you need the order, one method would be to keep your sortedKeysArray and use it as additional order of your dictionary. – makadev Apr 08 '16 at 15:24
  • how do i clear this issue now? – Bangalore Apr 08 '16 at 18:27

2 Answers2

1

You cannot sort an NSDictionary, it is an unsorted collection type. You will need to store your keys in an array and sort this and use it to access the NSDictionary in order.

Based on your code above, it could be modified as follows, e.g.

NSDictionary *dict = [NSDictionary dictionary];

NSArray *sortedKeys = [[dict allKeys] sortedArrayUsingSelector:@selector(compare:)];

for (NSString *key in sortedKeys) {
    NSLog(@"%@", [d objectForKey:key]);

    // Do something with the object here
}

Here you can pass around the sortedKeys array with the NSDictionary, and use the sortedKeys array for in-order access to your NSDictionary.

A more concise approach to get the array, but with the same outcome as above, would be using:

NSDictionary -keysSortedByValueUsingComparator as shown here.

Community
  • 1
  • 1
Tim
  • 8,932
  • 4
  • 43
  • 64
  • this is what i have done and it wont give you ordered data – Bangalore Apr 08 '16 at 18:28
  • This is not, what you have done, because he inserts the keys into an array, not an dictionary. BTW: I would chose an ordered set instead of an array. – Amin Negm-Awad Apr 08 '16 at 21:53
  • @Bangalore This isn't what you've done, I am sorting the keys into an array using the compare: selector as this is what you've chosen in your question. You then use the ordered array of IDs as a way to access the items in the dictionary in order. You cannot order an NSDictionary. – Tim Apr 09 '16 at 17:57
0

As others have mentioned, NSDictionaries cannot be sorted. However, you could do something like this:

-(NSArray *)sortedKeysFromDictionary:(NSDictionary *)dictionary ascending:(BOOL)ascending
{
    /* get all keys from dictionary */
    NSArray *allKeys = [dictionary allKeys];

    NSString *key = @"id"; // using "id" as key here

    /* sort keys */
    NSSortDescriptor *dateDescriptor = [NSSortDescriptor sortDescriptorWithKey:key ascending:ascending];
    return [NSArray arrayWithArray:[allKeys sortedArrayUsingDescriptors:@[dateDescriptor]]];
}

This will take all the keys from your dictionary, sort them in ascending or descending order as you desire and return that as an NSArray. This array can then be used to access the original dictionary. A sample implementation would look something like this:

for (NSString *key in [self sortedKeysFromDictionary:sampleDic ascending:NO])
{
    /* get value from current key */
    NSDictionary *currentDic = [sampleDic objectForKey:key];
}
Erik
  • 2,500
  • 6
  • 28
  • 49