Reordering Maps ( dictionary)

I was wondering why reordering maps in the editor wasn’t implemented, or at least alphabetically ordering keys. Please add this Epic !

2 Likes

If Blueprint Maps are based on tMaps, they are unordered by nature which is why they are so fast to access and act upon. If you want an ordered map, you should use two arrays instead.

I don’t think he’s asking for maps that auto sort, like the .Net sorted dictionary. Just a blueprint node that, given a map, returns a map that has the values sorted by the keys.

No, i’m not talking about them being ordered to use them as some ordered datatype, that would defeat the purpose of a tmap indeed.

I’m talking about how you add values IN THE EDITOR.

say, you add a few entries, and the key is a name… using some naming convention you come up with.

Then when you add an entry, you can’t have it alphabetically with the others, making it hard to keep track of what entrys you have.

I can use an enum, but if i need enums for every time i could use a simple name or int key… Datatables could work too, then i need a struct every time.

Which would be another feature request then… drag and drop enum / struct reordering.

That said, an iterator and ordering nodes wouldn’t be bad either, but you can get the keys and order / iterate those right.

It’s because they are a collection of key/value pairs;
There isn’t an easy index at hand to help sorting them.

Ah, and the editor can’t keep extra editor specific information about them? The editor has to display the entry’s in the map too right, so why can’t it itself go over the keys and sort them before it displays them ?

3 Likes

FrederickD is right, the order of the entries is saved in editor…it would be very nice to be able to re-order them, in editor.

3 Likes

If anyone is still looking for a way to sort a map by values from highest to lowest (while keeping the keys attached to the correct values), I figured out a blueprint way to do it. This example works with a map of two integers - for example playerID (key) and score (value). It’s a bit messy but it works. The map to be sorted in this BP is “Data Table Current Scores”, all other variables are made just for the purpose of this function. The sorted map is “Data Table Current Scores Sort Final”. (Ignore the third step in the Sequence node, that’s just what I do next with the sorted map for my game)

1 Like

Bit late to the party but I also had some trouble finding help with this topic. I’ve attached a screenshot of my solution which in hindsight, seems pretty obvious/straight forward.

  1. Start with a While Loop to iterate through the list until done
  2. I wanted to sort the “Values” of this map so use the Max of Int Array to grab the highest value entry in the Map
  3. Using that node, also grab the index of that entry which you’ll need shortly
  4. Now use the Get node and the previously grabbed Index value to make sure you’ve got the correct entry in the map.
  5. Now you just shove it into a new Map (local variable) using the index to make sure you’ve got the right entry. You’re basically just copying the Key and the Value straight into a new temporary map.
  6. Once its shoved in the new map, remove it from the original map so that the While Loop iterator can eventually finish once the Map is emptied.
  7. Extra spice, reason I was sorting was so they could be added to a scrollbox list in an Editor Utility Widget. Using a similar method of grabbing the data, I could then feed the new sorted list in order as child widgets of the scrollbox.

Hopefully someone finds this helpful. Shout out if something doesn’t make sense :slight_smile:

2 Likes