1

I am getting a dictionary of the following format :

[
  {
    "position": "1",
    "name": "Jaen",
    "group": "Student",
    "Address": "Delhi"
  },
  {
    "position": "2",
    "name": "Jaen",
    "group": "Student",
    "Address": "Delhi"
  },
  {
    "position": "1",
    "name": "Jaen",
    "group": "Teacher",
    "Address": "Delhi"
  }
]

Basically, I want to group them around the key named "group".I have to create a dictionary in the following format to populate data in my UI.

[
  {
    "Student": [
      {
        "position": "1",
        "name": "Jaen",
        "group": "Student",
        "Address": "Delhi"
      },
      {
        "position": "2",
        "name": "Jaen",
        "group": "Student",
        "Address": "Delhi"
      }
    ],
    "Teacher": [
      {
        "position": "1",
        "name": "Jaen",
        "group": "Teacher",
        "Address": "Delhi"
      }
    ]
  }
]

the order of the Groups should be maintained ie Student should come before Teacher. But when I am printing dictGroupField it is giving random results in terms of order.

How can i maintain the order?

Please find the code below that I was using :

    @property (strong, nonatomic) NSMutableDictionary *dictGroupField;


    -(void)loadFieldData {

    _dictGroupField = [[NSMutableDictionary alloc] init];  

     NSMutableArray *arrayFields = [self getArrayFields];

          for (NSDictionary *dictT in arrayFields) {
    NSString *strGroup = [dict valueForKey:@"group"];

    if ([_dictGroupField valueForKey:strGroup] == nil) {

                    NSMutableArray *arrGroup = [[NSMutableArray alloc] init];
                    [arrGroup addObject:dict];
                    [_dictGroupField setObject:arrGroup forKey:strGroup];
                } else {
                    NSMutableArray *arrGroupExist = [_dictGroupField valueForKey:strGroup];
                    [arrGroupExist addObject:dict];
                    [_dictGroupField setObject:arrGroupExist forKey:strGroup];
                }
    }

}
えるまる
  • 2,409
  • 3
  • 24
  • 44
Akaanksha
  • 11
  • 6
  • 4
    Dictionaries are unordered by definition. You could use an array and a custom class with a `group` property then you can sort the array by group. And don't use `valueForKey` unless you know what KVC is and you really need KVC. – vadian Sep 03 '19 at 09:31
  • format you shown is not dictionary its array of dictionaries. – Abu Ul Hassan Sep 03 '19 at 09:52

1 Answers1

0

You can't maintain it order.
Dictionaries are unordered collections of key-value associations. See more Docs

Thanh Vu
  • 1,599
  • 10
  • 14
  • 1
    Question is *How can i maintain the order?* not *Does dictionary is ordered?* – えるまる Sep 03 '19 at 09:37
  • @Erumaru You can't. Dictionaries are unordered collections of key-value associations. See more https://docs.swift.org/swift-book/LanguageGuide/CollectionTypes.html – Thanh Vu Sep 03 '19 at 09:38
  • 1
    I don't think that you should explain it to me. It's better to add it to your answer instead. – えるまる Sep 03 '19 at 09:40
  • This doesn't answer the question. The established solution (to this much-asked question) is to also maintain an array alongside the dictionary. – trojanfoe Sep 03 '19 at 10:12
  • I am seeking a solution. I know Dictionaries are unordered and i cant be used in this scenario. – Akaanksha Sep 04 '19 at 15:41