for loop with sorted dictionary keys

0 favourites
  • 9 posts
From the Asset Store
SynthWave Loop Pack includes 68 seamless loops, founded on 11 original melodies.
  • Hi,

    It would be great if it were possible to have the keys in a dictionary sorted during a loop execution. While dictionaries per definition do not define a sort order for keys -- dictionaries are merely associative storage, it would nevertheless be useful to be able to travers all keys in a sorted manner.

    Right now, I load the dictionary keys into an array, sort the array, and then loop through the array and access the dictionary via the the keys sorted in the array -- it works well, but is rather memory intensive and cumbersome.

    thanks,

    Dan

  • They don't need to be sorted because they're named unlike arrays which require an order/position. If you want to sort them you could add a number to their name and use a normal for each loop and get them like that, otherwise why not use an array to begin with?

    (not actual code)

    add key "item0" set value whatever.

    add key "item1" set value whatever….

    for "sort" from 0 to key count

    Dictionary.Get("item"&loopindex("sort")) and do whatever.

  • Thanks Ethan,

    I do use the keys of a Dictionary, for example, to store player names, and dictionary values, to store some values associated with each player, say, number of time the player played a game.

    When outputting this information, its useful to have the players sorted -- despite being internally stored in a dictionary.

    Dan

  • So your wanting to sort them based on the stored value. I see your point now.

  • Not exactly, sort them by player name -- I guess you meant that ...

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Maybe create multiple 'Player' dictionaries at runtime, one per player? You can then pick them by giving the dict an instance var and storing the player name/id in there.

  • I want to make a top 10 highscore screen, and I currently use a dictionary to save scores ("name of player",score). All I want is to show a screen with the top 10 sorted by the score and showing the name associated to it. I simply don't know how to do it in construct...

  • I know I shouldn't necro, but I've looked at the FAQ, done quite a lot of searching and I'm having exactly this problem and want to do exactly the same thing.

    Another way round it would be if I could access the name of the key by value (copy values to array, sort, then get keys by iterating back through the array), which also seems impossible.

    Any ideas? I'm using C3 if that makes a difference (which I would imagine it doesn't).

  • You could put all the dictionary keys in an array, sort the array, then loop over the array to get keys in order.

    set array size to (0,1,1)

    dictionary: for each key

    --- array: push key to end

    array: sort

    array: for each x

    --- dictionary: get array.curValue

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)