Resolved Sorting NSDictionary

Discussion in 'iOS Programming' started by Scott90, Apr 11, 2011.

  1. Scott90, Apr 11, 2011
    Last edited: Apr 12, 2011

    Scott90 macrumors 6502

    Jul 14, 2008
    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!
  2. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    Seems like keysSortedByValueUsingSelector is your friend.

    Read the Sort A Dictionary section in the Collections Programming Topics guide.
  3. Scott90 thread starter macrumors 6502

    Jul 14, 2008
    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).
  4. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    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.
  5. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    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",
    NSArray *sortedKeysArray =
        [dict keysSortedByValueUsingSelector:@selector(compare:)];
    // sortedKeysArray contains: (Geography, History, Mathematics, English)
    This is from the collections programming topics that I mentioned.
  6. Scott90 thread starter macrumors 6502

    Jul 14, 2008
    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.
  7. dejo Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State

    Does that help? ;) Sorry for the ribbing. It's meant to be gentle.
  8. Scott90 thread starter macrumors 6502

    Jul 14, 2008
    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.
  9. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    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.
  10. Scott90 thread starter macrumors 6502

    Jul 14, 2008
    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).

Share This Page