Why are NSDictionary keys sorted alphabetically

I am using NSDictionary from a tutorial that I found, but I had a quick question as to why it is 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.

+4
source share
1 answer

Because you sort them alphabetically?

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

Note that dictionary keys are inherently unordered. If you need some sort of sort order applied to keys, you will need to save it separately from the dictionary.

+18
source

Source: https://habr.com/ru/post/1341565/


All Articles