Hello!
I have an NSMutableDictionary with 7 objects and 7 keys
I than put them into an Array and when i display the array, it displays in a random order. Any idea how I can display this information in the order in which i typed it out?
Code Snippet:
-(void)setupArray{
episodes = [[NSMutableDictionary alloc]init];
[episodes setObject: @"Info for ep 1" forKey: @"Ep 1"];
[episodes setObject: @"Info for ep 2" forKey: @"Ep 2"];
[episodes setObject: @"Info for ep 3" forKey: @"Ep 3"];
[episodes setObject: @"Info for ep 4" forKey: @"Ep 4"];
[episodes setObject: @"Info for ep 5" forKey: @"Ep 5"];
[episodes setObject: @"Info for ep 6" forKey: @"Ep 6"];
[episodes setObject: @"Info for ep 7" forKey: @"Ep 7"];
datasource = [episodes allKeys];
}
When it displays it display
Ep 6
Ep 2
Ep 3
Ep 7
Ep 4
Ep 5
Ep 1
Any help would be greatly appreciated!
Thanks in advance
- Jason
Well you'll never be able to sort it back into the order you typed it in, there will only be coincidences when you sort them. So don't rely on the order in dictionaries.
__________________
If you are looking for a quality developer, I'm your man. Give me a PM if you are interested.
Hello!
I have an NSMutableDictionary with 7 objects and 7 keys
I than put them into an Array and when i display the array, it displays in a random order. Any idea how I can display this information in the order in which i typed it out?
Code Snippet:
-(void)setupArray{
episodes = [[NSMutableDictionary alloc]init];
[episodes setObject: @"Info for ep 1" forKey: @"Ep 1"];
[episodes setObject: @"Info for ep 2" forKey: @"Ep 2"];
[episodes setObject: @"Info for ep 3" forKey: @"Ep 3"];
[episodes setObject: @"Info for ep 4" forKey: @"Ep 4"];
[episodes setObject: @"Info for ep 5" forKey: @"Ep 5"];
[episodes setObject: @"Info for ep 6" forKey: @"Ep 6"];
[episodes setObject: @"Info for ep 7" forKey: @"Ep 7"];
datasource = [episodes allKeys];
}
When it displays it display
Ep 6
Ep 2
Ep 3
Ep 7
Ep 4
Ep 5
Ep 1
Any help would be greatly appreciated!
Thanks in advance
- Jason
A dictionary is not an ordered list. If you want a particular order, use an array, or an array of dictionaries.
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
you probably need to load the keys into an array first. or to display, something like this should work:
for(NSString *key in [[myDict allkeys] sortedArrayUsingSelector:@selector(compare]) {
NSLog(key);
}
My datasource is an array, that is populated with with Dictionary of Episodes 1-7
so how would i sort them using the @: selector(compare method? like what goes in the curly braces?
Thanks for your help