I'm trying out the method Array::sort_custom.
https://docs.godotengine.org/en/stable/classes/class_array.html#class-array-method-sort-custom
Here's my code:
extends Node
class Foo:
var name: String
var value: int
func _init(x: String, y: int) -> void:
name = x
value = y
func sort(a: Foo, b: Foo) -> bool:
print("sort_foo: a.name=%s, a.value=%s, b.name=%s, b.value=%s" % [a.name, a.value, b.name, b.value])
return (a.value > b.value)
var foos: Array = [Foo.new("foo1",3), Foo.new("foo2",9), Foo.new("foo3",6)]
func _init() -> void:
print_foos_array("Before", foos)
foos.sort_custom(Foo, 'sort')
print_foos_array("After", foos)
func print_foos_array(label: String, x: Array) -> void:
print(label)
for f in x:
print("(%s, %s)" % [f.name, f.value])
Here's the output:
Before
(foo1, 3)
(foo2, 9)
(foo3, 6)
After
(foo1, 3)
(foo2, 9)
(foo3, 6)
The array doesn't get sorted. The custom sort function is apparently never called. The print inside it produces no output, and if I set a breakpoint there, it's not hit.
Is there a mistake in my code?