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

Mockup & CodeGen, iPhone & iPad
($9.99)

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

Manu
($0.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 03-02-2009, 10:14 AM   #1 (permalink)
New Member
 
Join Date: Feb 2009
Posts: 11
Default Sort NSDictionary

Anyone has sample code to show how to sort a NSDictionary (keys/values).
I can get the keys and values out in two separate NSArrays but how can I sort both simultaneously.

Anyone has done this before?
fontecir is offline   Reply With Quote
Old 03-02-2009, 11:48 AM   #2 (permalink)
Registered Member
 
RickMaddy's Avatar
 
Join Date: Oct 2008
Location: Denver, CO
Posts: 2,122
Default

You can't really. What are you actually trying to accomplish with the keys and values?

You can get the keys as an array and sort it. Then iterate over the sorted key array and use each key to get the value from the original dictionary. But I don't know if that solves what you are trying to do.
RickMaddy is offline   Reply With Quote
Old 03-02-2009, 11:59 AM   #3 (permalink)
New Member
 
Join Date: Feb 2009
Posts: 11
Default Let me explain...

Quote:
Originally Posted by RickMaddy View Post
You can't really. What are you actually trying to accomplish with the keys and values?

You can get the keys as an array and sort it. Then iterate over the sorted key array and use each key to get the value from the original dictionary. But I don't know if that solves what you are trying to do.
Here is the whole story:

1. I retrieve elements from my SQLITE database sorted and place these elements in a array of objects. All good here.

2. I then traverse the array using an NSEnumerator

Right here I realized I lost the sorting I got from the database. Somehow the Enumerator sorts by default on when the objects were created.

So now I'm trying to resort the array because I dont want to pay for going to the database.

So, my real question is how can I traverse the array without loosing the order.
fontecir is offline   Reply With Quote
Old 03-02-2009, 12:17 PM   #4 (permalink)
Registered Member
 
RickMaddy's Avatar
 
Join Date: Oct 2008
Location: Denver, CO
Posts: 2,122
Default

If the items were already sorted as you added each item to the array then they are still ordered in the array (and why did you ask about dictionaries?).

Just do this:

Code:
// someSortedArray is your array with the sorted data from the database
// Replace 'MyObject' with whatever is really in your array
for (MyObject *obj in someSortedArray) {
    // Do something with 'obj'
}
RickMaddy is offline   Reply With Quote
Old 03-02-2009, 12:19 PM   #5 (permalink)
Pro. Game Developer
iPhone Dev SDK Supporter
 
Join Date: Feb 2009
Location: żLa Islas Hermosas?
Posts: 2,178
Default

Quote:
Originally Posted by fontecir View Post
Here is the whole story:

1. I retrieve elements from my SQLITE database sorted and place these elements in a array of objects. All good here.

2. I then traverse the array using an NSEnumerator

Right here I realized I lost the sorting I got from the database. Somehow the Enumerator sorts by default on when the objects were created.

So now I'm trying to resort the array because I dont want to pay for going to the database.

So, my real question is how can I traverse the array without loosing the order.
You can traverse an NSArray using plain-old 0 through (n-1) indexing. Search the docs. Assuming your entries are added to the array pre-sorted, they should remain sorted while in the array.
Kalimba is offline   Reply With Quote
Old 03-02-2009, 12:27 PM   #6 (permalink)
New Member
 
Join Date: Feb 2009
Posts: 11
Default Solved!

Quote:
Originally Posted by RickMaddy View Post
If the items were already sorted as you added each item to the array then they are still ordered in the array (and why did you ask about dictionaries?).

Just do this:

Code:
// someSortedArray is your array with the sorted data from the database
// Replace 'MyObject' with whatever is really in your array
for (MyObject *obj in someSortedArray) {
    // Do something with 'obj'
}
So why people use Enumerators for, if you can traverse the Array this way?

Thanks this will solved my problem. I had created a dictionary of a couple of elements (i'm retrieving many from the DB) as I was traversing the array to display in another view and I was loosing the sort.
fontecir is offline   Reply With Quote
Old 08-22-2009, 01:28 AM   #7 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 1
Default

Quote:
Originally Posted by fontecir View Post
Anyone has sample code to show how to sort a NSDictionary (keys/values).
Here's a sample method which does that:

BravoBug News Blog Archive Cocoa Quickies: Sort an NSDictionary by keys

(Sorry to dig up an old thread, just thought others might still find this helpful)
BBug is offline   Reply With Quote
Reply

Bookmarks

Tags
nsarray, nsdictionary, sort

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: 250
22 members and 228 guests
@sandris, ADY, Dani77, diyora, FAED, fredidf, F_Bryant, iDifferent, JamesCahall, JasonR, mer10, Oral B, prchn4christ, Rudy, smithdale87, Speed, spiderguy84, stekki, tgjorgoski, Touchmint, twerner, vigu360
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,880
Threads: 89,228
Posts: 380,755
Top Poster: BrianSlick (7,129)
Welcome to our newest member, @sandris
Powered by vBadvanced CMPS v3.1.0

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