Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my NSDictionary keys sorted alphabetically

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.

like image 507
James Dunay Avatar asked Dec 03 '22 03:12

James Dunay


1 Answers

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.

like image 69
bbum Avatar answered Dec 20 '22 15:12

bbum