The NSDictionary allKeys array is corrupted - not in the order they are in the dictionary?

in my project, I pull data from .plist, more specifically from NSDictionary. I am running a dictionary with .plist content that works well. When i do then

NSArray *array = [dict allKeys];

it fills the array with all the keys, but in a completely random order, different from the order that they are in the .plist file. I really need to save the order. Keys in the dictionary are arrays if this can cause a problem. What am I not getting?

Many thanks!

+5
source share
5 answers

Just as there is no order between elements in an NSSet, there is no inherent order for key-value pairs in an NSDictionary.

, - - (NSArray *)keysSortedByValueUsingComparator:(NSComparator)cmptr, , NSDictionary plist.

+7

, . . .

NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:
                       @"01.Created",@"cre",
                       @"02.Being Assigned",@"bea",
                       @"03.Rejected",@"rej",
                       @"04.Assigned",@"ass",
                       @"05.Scheduled",@"sch",
                       @"06.En Route",@"inr",
                       @"07.On Job Site",@"ojs",
                       @"08.In Progress",@"inp",
                       @"09.On Hold",@"onh",
                       @"10.Completed",@"com",
                       @"11.Closed",@"clo",
                       @"12.Cancelled", @"can",
                       nil]; 

, sortingArrayUsingSelector, , .

 NSArray *arr =  [[dict allKeys] sortedArrayUsingSelector:@selector(localizedStandardCompare:)];

, UIView, 3.

+6

, NSDictionary . , .plist. .

+3

You can create an NSArray with the dictionary keys in the order you want.

+2
source

Try the following:

NSArray *array = [dict allKeys];

NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"self" ascending:NO];
NSArray *descriptors = [NSArray arrayWithObject:descriptor];

NSArray *orderedKeys = [array sortedArrayUsingDescriptors:descriptors];
-1
source

All Articles