4

Im using an NSDictionary from a tutorial that i found, but I just had a quick question as to why it is being sorted alphabetically?

NSArray *array = [[NSArray alloc] initWithObjects:userName, nil];
    NSArray *array2 = [[NSArray alloc] initWithObjects:@"Other Data", @"Second Entry", nil];

    NSDictionary *temp =[[NSDictionary alloc] initWithObjectsAndKeys:array, @"Name", array2, @"A",nil];

    self.sortedKeys =[[temp allKeys] sortedArrayUsingSelector:@selector(compare:)];

Thanks for the help.

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
James Dunay
  • 2,714
  • 8
  • 41
  • 66

1 Answers1

18

Because you are sorting them alphabetically?

self.sortedKeys =[[temp allKeys] sortedArrayUsingSelector:@selector(compare:)];

Note that a dictionary's keys are inherently unordered. If you need some kind of sort order applied to the keys, you'll need to maintain it yourself separately from the dictionary.

bbum
  • 162,346
  • 23
  • 271
  • 359
  • 1
    If I just wanted them to be sorted as entered how might i do that? – James Dunay Feb 28 '11 at 19:58
  • 1
    There’s no way to make the standard `NSDictionary` keep the keyes ordered, see this [related question](http://stackoverflow.com/questions/376090). – zoul Feb 28 '11 at 21:00