-1

to start, this is my code :

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL
                                                          URLWithString:url]];

    NSData *response = [NSURLConnection sendSynchronousRequest:request
                                             returningResponse:nil error:nil];

    NSDictionary *publicTimeline = [NSJSONSerialization JSONObjectWithData:response       options:0 error:&jsonParsingError];

    for (NSObject* key in publicTimeline) {
        id value = [publicTimeline objectForKey:key];
        NSLog(@"%@", key);
    }

I take few news on a webservice, and i show it in a tableView.

My problem is that these news aren't show in order.

In my webservice, the news are in order, for example i have :

    {"0":{"title":"News1"}}
    {"1":{"title":"News2"}}
    {"2":{"title":"News3"}}

etc..

but in the loop, i "loose" this order :/ And i want to have my news in this order, first the news with the index "0" after "1" etc...

(the NSDictionary seems to loose this order, for example, after my code, i have in my tableview, the news : "2", "0", "1", and not my news "0", "1", "2". )

(i tried some answers, but none seems to work in my case :( )

SOmeone to help me ? thx,

deveLost
  • 1,151
  • 2
  • 20
  • 47
  • What you mean by index? I can see nested Dictionary? What you want exactly? – Mani Feb 20 '14 at 10:52
  • By index, i want to mean the number present in my webservice, here it is "0", "1", "2", etc... – deveLost Feb 20 '14 at 10:53
  • And the problem is, the NSDictionary seems to loose this order, for example, after my code, i have in my tableview, the news : "2", "0", "1", and not my news "0", "1", "2". – deveLost Feb 20 '14 at 10:54

3 Answers3

5

This is what I used last time to sort the keys of my dictionary. Hope it will be easier for you to implement it :)

NSMutableArray *sortedArray = [NSMutableArray arrayWithArray:publicTimeline.allKeys];
[sortedArray sortUsingSelector:@selector(localizedStandardCompare:)];

And you should have your sorted keys in sortedArray

stellz
  • 130
  • 1
  • 8
  • YEAH ! Thanks a lotttt, this is the solution :D – deveLost Feb 20 '14 at 14:49
  • Oh no :D Indeed it's in order, but there is a problem, because per example, when i do that : for (int i = 0; i < sortedArray.count; i++) { id value = [sortedArray objectAtIndex:i]; NSLog(@"%@",[value objectForKey:@"title"]); } i have an error :( it seems it doesn't found title in the object. – deveLost Feb 20 '14 at 14:58
  • In the sortedArray you have the **keys** sorted. That said, you should do: `for (int i = 0; i < sortedArray.count; i++) { NSString *key = [sortedArray objectAtIndex:i]; NSLog(@"%@",[publicTimeline objectForKey:key]); } And this will print the value, corresponding to the key in your publicTimeline dictionary. Hope that explains it well :)` – stellz Feb 20 '14 at 15:07
  • Yes, you right :) it's running :) Thx you, and thx all :) I use "for (id key in sortedArray) {" it's better, as well not necessary to declare and use a "NSString *key" – deveLost Feb 20 '14 at 15:09
3

You can work with that :

NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:publicTimeline];
[dict keysSortedByValueUsingComparator:^(id obj1, id obj2) {
    return (NSComparisonResult)[obj1 compare:obj2];
}];

And then USE dict instead of publicTimeline.

Hope that will help.

Nicolas Bonnet
  • 1,275
  • 11
  • 15
  • Nop, it's not running, it doesn't find "keysSortedByValueUsingComparator", i forgot something ? – deveLost Feb 20 '14 at 11:02
  • Use `NSMutableDictionary` instead of `NSDictionary` – Nicolas Bonnet Feb 20 '14 at 11:07
  • Ok, i did the update, and i set your code, but i have an error -[__NSCFDictionary compare:]: unrecognized selector sent to instance 0xbab33e0 – deveLost Feb 20 '14 at 13:09
  • As I can see `__NSCFDictionary` is not mutable ... `NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:publicTimeline];` – Nicolas Bonnet Feb 20 '14 at 13:22
  • i need more details, i don't understand what you mean :( If i add your code before to do kessortedbyvalucomparator, i have the same error. – deveLost Feb 20 '14 at 13:38
  • ... publicTimeline is not 'mutable' as a `NSDictionary` and to use `keysSortedByValueUsingComparator ` that must be a `NSMutableDictionary`. I edit my response for your case... – Nicolas Bonnet Feb 20 '14 at 13:41
  • Ok, i understand that, but i have an error again : -[__NSCFDictionary compare:]: unrecognized selector sent to instance 0xa5c2b50 – deveLost Feb 20 '14 at 13:46
2

Dictionaries don't have an order. Just because the server generates your JSON in a particular order doesn't mean that it is / can be maintained when you deserialise with NSJSONSerialization.

If you need to maintain the order, either:

A. Get all of the keys from the dictionary and sort them. Then, any time you need to access in order (by index), get the key from the array and use that (don't iterate the dictionary).

B. Use a different method to deserialise the JSON which can keep / provide data about the order in which things were processed (RestKit can do that for you).

Wain
  • 118,658
  • 15
  • 128
  • 151