Ordering / sorting objects in a dictionary?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Robster
:warning: Old Version Published before Godot 3 was released.

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()
	dict.parse_json(text)
	file.close()

	#load the appropriate staff
	loadStaff()


func loadStaff():
	for i in dict:
		filteredDict.append(i)
	
	#sort the array attempt, but I don't know how to make it sort exactly by JSON data type
	#filteredDict.sort()
	

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…

:bust_in_silhouette: Reply From: Zylann

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.

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.

Robster | 2017-12-11 01:04