I use Godot 3.

Hello, Lets's say I have the following array and it is structured like this.... slots[0] = "" slots{1] = 11 slots[2] = 1 slots[3] = "" slots[4] = 4

But I need some type of code that ill actrually sort the array structure so it ends up like this instead... slots[0] = 1 slots{1] = 4 slots[2] = 11 slots[3] = "" slots[4] = ""

Please help?

Use sort_custom of Array. Here's an example:

class MyCustomSorter:
	static func sort(a, b):
		if typeof(b)==TYPE_STRING || (typeof(a)!=TYPE_STRING && typeof(b)!=TYPE_STRING && a<b):
			return true
		return false

func _ready():
	var my_items = ["",11,1,"",4]
	my_items.sort_custom(MyCustomSorter, "sort")
	printt(my_items)

The sort function has to return true if the first element is less than the second one and has to be swapped or false otherwise.

4 years later