PDA

View Full Version : [Resolved] Sorting NSDictionary




Scott90
Apr 11, 2011, 11:43 PM
I know the title is a bit misleading, because this is just not possible. It is, however, what I would like to do, one way or another.

I'm keeping a high score list, the keys being the names, the values the scores. Whenever someone submits a new score, that score is being added to the dictionary. However, I want to sort (descending) that dictionary when I'm displaying the scores, so that the highest score is up top.

Now, I know this is not possible using NSDictionary, but what would be a good and efficient way to do this? I open for anything, the names and scores are being stored in NSUserDefaults, so anything that NSUserDefaults can handle, I can handle.

I don't think I really need code (unless it gets really involved, then some code may help), but a general idea of how to implement this would be awesome!



PhoneyDeveloper
Apr 11, 2011, 11:50 PM
Seems like keysSortedByValueUsingSelector is your friend.

Read the Sort A Dictionary section in the Collections Programming Topics guide.

Scott90
Apr 12, 2011, 12:07 AM
Can't use that, I want to sort by value which doesn't seem to be possible. The keys are the names entered, the values are the scores. I can't switch those, because then a score can only be entered once (two people can't have a score of 8, because of the uniqueness of keys).

chown33
Apr 12, 2011, 12:49 AM
NSDictionary has an allValues method. It returns NSArray. Sort the array.

You could construct a reversed dictionary: the keys are scores, the values are arrays of names (i.e. each name in the array has the same score).

You could just keep an array of name:score pairs. That would make it easier to keep only the top 10 scores. Only add a name:score pair if it's greater than the lowest score, sort the list, remove the low scores until 10 or fewer remain.

PhoneyDeveloper
Apr 12, 2011, 11:44 AM
Is this not what you want?

NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:63], @"Mathematics",
[NSNumber numberWithInt:72], @"English",
[NSNumber numberWithInt:55], @"History",
[NSNumber numberWithInt:49], @"Geography",
nil];

NSArray *sortedKeysArray =
[dict keysSortedByValueUsingSelector:@selector(compare:)];
// sortedKeysArray contains: (Geography, History, Mathematics, English)

This is from the collections programming topics that I mentioned.

Scott90
Apr 12, 2011, 12:04 PM
Is this not what you want?

NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:63], @"Mathematics",
[NSNumber numberWithInt:72], @"English",
[NSNumber numberWithInt:55], @"History",
[NSNumber numberWithInt:49], @"Geography",
nil];

NSArray *sortedKeysArray =
[dict keysSortedByValueUsingSelector:@selector(compare:)];
// sortedKeysArray contains: (Geography, History, Mathematics, English)

This is from the collections programming topics that I mentioned.

Hey, that IS what I want! The name threw me off, keysSortedByValueUsingSelector made me think it sorted by keys, but it appears that it sorts by value and returns the keys. Thanks, that helped a lot!

And chown33, thanks for your answer also, of course.

dejo
Apr 12, 2011, 12:41 PM
The name threw me off, keysSortedByValueUsingSelector made me think it sorted by keys, but it appears that it sorts by value...

keysSortedByValueUsingSelector

Does that help? ;) Sorry for the ribbing. It's meant to be gentle.

Scott90
Apr 12, 2011, 01:45 PM
keysSortedByValueUsingSelector

Does that help? ;) Sorry for the ribbing. It's meant to be gentle.

Haha, it's all good! :) The fact that I read somewhere that dictionaries couldn't be sorted didn't really help either, didn't really look any further after that.

I thought by value they meant the value of the key, not the corresponding value for the key in the dictionary. It does make sense now, though.

PhoneyDeveloper
Apr 12, 2011, 01:47 PM
I gotta say, I hate it when I answer a beginner's question and they tell me I'm wrong. I did hesitate to explain further.

Scott90
Apr 12, 2011, 02:37 PM
My apologies, didn't mean to annoy you. When you're new to a forum, you don't know the users so you can't tell the experts from the "trying to help, but really have no idea what they're talking about" kind of users. That, in combination with the, apparently wrong information about not being able to sort a dictionary, made me think that wasn't the method I was looking for.

Please do continue helping us beginners, message boards are, at least for me, the number one way to learn new things (I would even put it above the documentation).