How do I sort a scroll container's contents?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By lee451

I’m trying to sort by ascending using a ScrollContainer, but I don’t think I’m doing it correctly.

In my scene I have this kind of parent-child relationship

Node (Main)

  • ScrollContainer
    • VBoxContainer
      • HBoxContainer
        • ColorRect
        • TextEdit
        • Button

Where I have multiple HBoxContainers in the VBoxContainer. The TextEdit control has different string values that I’m converting to floats in my custom sort function.

func _on_ScrollContainer_sort_children():
	$ScrollContainer.get_node("VBoxContainer").get_children().sort_custom(MyCustomSorter, "sort")

class MyCustomSorter:
	static func sort(a, b):
		if float(a.get_node("TextEdit").text) < float(b.get_node("TextEdit").text):
			print(a.get_node("TextEdit").text + " is less than " + b.get_node("TextEdit").text + " returned true")
			print(a.get_node("TextEdit").text + " is greater than or equal to " + b.get_node("TextEdit").text + " returned false")
		return float(a.get_node("TextEdit").text) <= float(b.get_node("TextEdit").text)

The function seems to be getting called because it does print out to the console, but the controls don’t seem to be getting rearranged.

Am I doing this completely wrong? Is there example code out there that already does something like this? Did I miss a setting?

I appreciate any help.

:bust_in_silhouette: Reply From: lee451

Eventually found a way to sort, but it doesn’t seem like a clean way to do it.

If anyone has a better way feel free to share.

func sort_list():
var child_list = $ScrollContainer/VBoxContainer.get_children()
var num_children = child_list.size()
for ii in range(0, num_children):
	for i in range(0, num_children):
		if i+1 < num_children:
			if float($ScrollContainer/VBoxContainer.get_child(i).get_node("TextEdit").text) > float($ScrollContainer/VBoxContainer.get_child(i+1).get_node("TextEdit").text):

Thank you for the code :slight_smile:

MelkMoon | 2020-06-10 17:32