Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 11-21-2011, 11:32 AM   #1 (permalink)
Registered Member
 
Join Date: Nov 2011
Posts: 105
jasonbfly is on a distinguished road
Default Sort NSMutableDictionary

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
jasonbfly is offline   Reply With Quote
Old 11-21-2011, 05:33 PM   #2 (permalink)
Registered Member
 
Join Date: Dec 2010
Location: Seattle, WA
Posts: 408
RickSDK is on a distinguished road
Default

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);
}
RickSDK is offline   Reply With Quote
Old 11-21-2011, 05:58 PM   #3 (permalink)
Just helping out.
 
Domele's Avatar
 
Join Date: Feb 2011
Posts: 2,565
Domele is on a distinguished road
Default

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.

New app - See screenshots and details at www.globaclock.com.

If you want to thank me, click the link. Every click counts. If you want to do more, buy my app. A link is available on my website. Thanks.
Domele is offline   Reply With Quote
Old 11-21-2011, 06:44 PM   #4 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by jasonbfly View Post
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.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


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.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is online now   Reply With Quote
Old 11-21-2011, 09:33 PM   #5 (permalink)
Registered Member
 
Join Date: Nov 2011
Posts: 105
jasonbfly is on a distinguished road
Default

Quote:
Originally Posted by RickSDK View Post
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
jasonbfly is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 389
15 members and 374 guests
7twenty7, chiataytuday, Clouds, dedeys78, Duncan C, e2applets, EvilElf, iekei, ipodphone, jeroenkeij, leostc, mbadegree, Murphy, QuantumDoja, Sami Gh
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,676
Threads: 94,125
Posts: 402,910
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jleannex55
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 06:33 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0