Description
Describe the project you are working on
A game with a lot of dynamic code.
Describe the problem or limitation you are having in your project
Currently, there is no way to sort a Dictionary to make the keys be in order:
func _ready() -> void:
var dict = {
"key4": "value4",
"key2": "value2",
"key1": "value1",
"key3": "value3",
}
print(dict) # { "key4": "value4", "key2": "value2", "key1": "value1", "key3": "value3" }
I need the ability to do this so that I can have consistency between the in-memory data and what I store on MongoDB, which automatically sorts all Dictionaries I send to it. I also need it so that I can convert a Dictionary to a String and have identical contents result in identical strings.
Describe the feature / enhancement and how it helps to overcome the problem or limitation
I propose adding a sort
method so you can do this:
func _ready() -> void:
var dict = {
"key4": "value4",
"key2": "value2",
"key1": "value1",
"key3": "value3",
}
dict.sort()
print(dict) # { "key1": "value1", "key2": "value2", "key3": "value3", "key4": "value4" }
Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams
godotengine/godot#77213 Add a sort
method to Dictionary and HashMap and expose it.
If this enhancement will not be used often, can it be worked around with a few lines of script?
It can be worked around in GDScript, but not efficiently, and not in-place. I don't know how often it will be used.
Is there a reason why this should be core and not an add-on in the asset library?
It's a computation-heavy operation and very related to Dictionary, so it would be most optimal to put it in core.