+1 vote

Godot V 2.1.4 stable Linux

Hi all,

I am making a game where I have staff that I can employ. Their details are saved in a JSON file and when loaded, I want to order them in a particular way, based on the JSON data. For example:

var dict = {}

func _ready():

    #load ALL the JSON staff data for use in functions below
    var file = File.new()
    file.open("res://json/staff.json", file.READ)
    var text = file.get_as_text()

    #load the appropriate staff

func loadStaff():
    for i in dict:

    #sort the array attempt, but I don't know how to make it sort exactly by JSON data type

My JSON data looks a little like this:

  "0" : {
    "name": "James Rashburn",
    "image": "staff-James.png",
    "hired": false,
    "referral": "internal",
    "referralPrice": 0,
    "1" : {
    "name": "Emma Smith",
    "image": "staff-Emma.png",
    "hired": false,
    "referral": "external",
    "referralPrice": 0,

So I have lots of staff (more than just the two above) but I want them to sort by "referral" type in the JSON data. So all the external are grouped together and all the internal are grouped together.

Is there a way to do this?

Any advice much appreciated...

in Engine by (825 points)

1 Answer

+3 votes
Best answer

JSON dictionaries are defined by the official spec to be unordered.
Also, in general, many programming languages design dictionaries as being unordered (hash maps).

In Godot, dictionaries also are hash maps, but recently a feature was added for them to be ordered in 3.0 if you iterate on them with a for loop (but they are still hash maps!). This order is based on which element was inserted last, but I'm not aware of any further sorting being implemented for this.

But even with the feature above, JSON will not guarantee the order for you, unless you don't use JSON, or don't use dictionaries in the first place.
Arrays are better suited for ordered data. If you believe dictionaries must have sorting capabilities, you should ask for a feature on Github.

One way to sort by referral regardless of JSON would be to copy all your items in an array, then sort the array using sort_custom, and re-create your dictionary by inserting the elements in the same order you get them from the sorted array.

Personally, even with 3.0 dictionaries, I would still use arrays for any kind of sorted data because these are ordered by design and have functions to do so.

by (28,833 points)
selected by

Thanks for the solid answer. I'll move the ID and sorting data (referral) into an array. Sort the array based on the referral, then I'll use the order of the sorted array to load the dict data using the ID. I hope that made sense.

Thanks again Zylann, much appreciated.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.