0

So I have this NSDictionary like so:

NSDictionary *productionSchedule = [[[NSMutableDictionary alloc]initWithDictionary:[[areaData GetProductionScheduleData:communityDesc] objectForKey:@"Root"]] autorelease];

The data for the NSDictionary is coming from an API and due to the fact that NSDictionary does not do ordering, the order of the data in API is different in the NSDictionary, so now I am trying to put the keys of the NSDictionary into an NSMutableArray to handle the ordering. In my NSDictionary I have a value called SortOrder and I am trying to put the data in NSDictionary into NSMutableArray based on this value SortOrder (I have about 389 items and the SortOrder goes from 0 - 389) How would I do this?

I have this screenshot that will show you what my data is like:

enter image description here

What I am trying to do is put the key 'V3C0183' but as the 82nd item (there will be 81 items before this)

I am assuming I will have to do a foreach loop like so:

NSMutableArray *prodSchedSortedKeys

for(int i = 0;i<[productionSchedule count];i++)
{

[prodSchedSortedKeys addObject: ? ];

}

I just dont know what the next step would be to add an object based off the sort order....please help.

user979331
  • 11,039
  • 73
  • 223
  • 418
  • I don't see anything new in the topic you propose. Sorting an array has been asked many times. I suggest you help yourself by running search. – El Tomato Jun 08 '16 at 02:39
  • Is your key also in your `value` dictionary? – Paulw11 Jun 08 '16 at 02:45
  • the key value is an NSDictionary also and that value has a key called SortOrder and that has its own value – user979331 Jun 08 '16 at 02:47
  • Unrelated but why are the values for "SortOrder" strings instead of numbers? – rmaddy Jun 08 '16 at 02:51
  • @rmaddy in your answer (which was deleted for some reason) order1 and order2 return nil each time – user979331 Jun 08 '16 at 03:18
  • NSDictionary is type of collection unordered, you need sort it your self base on keys or value. Another way is server return Array. Array is of ordered collection. – Trung Phan Jun 08 '16 at 05:02

2 Answers2

0
NSDictionary *dic = //your dictionary;

NSArray<NSDictionary *> *values = dic.allValues;
[values sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
    return [obj1[@"SortOrder"] integerValue] > [obj2[@"SortOrder"] integerValue];
}];

Now the dic is ordered.

But this is not the best solution of this problem. Server's data should be ordered instead, that the key like "VC31083" should in the key-value pairs too.

Edit1: sortedArrayUsingComparator: is used for normal sort of array, the performance isn't very well if the content is too large. Especially in this compare, it do addition actions: get value from dictionary, transform NSString to int, and then compare. You can Log to see how much time it spend on this sort with your data.

Klein Mioke
  • 1,261
  • 2
  • 14
  • 23
-1

NSMutableDictionary/NSDictionary can't do that. Take a look at e.g. Matt Gallaghers OrderedDictionary.

Also take a look at this answer: Getting NSDictionary keys sorted by their respective values

Community
  • 1
  • 1
maddy
  • 4,001
  • 8
  • 42
  • 65