James Dunay
James Dunay

Reputation: 2724

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.

Upvotes: 4

Views: 6319

Answers (1)

bbum
bbum

Reputation: 162712

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.

Upvotes: 18

Related Questions