|
@@ -477,15 +477,19 @@
|
|
[b]Note:[/b] You cannot randomize the return value as the heapsort algorithm expects a deterministic result. Doing so will result in unexpected behavior.
|
|
[b]Note:[/b] You cannot randomize the return value as the heapsort algorithm expects a deterministic result. Doing so will result in unexpected behavior.
|
|
[codeblocks]
|
|
[codeblocks]
|
|
[gdscript]
|
|
[gdscript]
|
|
- class MyCustomSorter:
|
|
|
|
- static func sort_ascending(a, b):
|
|
|
|
- if a[0] < b[0]:
|
|
|
|
- return true
|
|
|
|
- return false
|
|
|
|
|
|
+ func sort_ascending(a, b):
|
|
|
|
+ if a[0] < b[0]:
|
|
|
|
+ return true
|
|
|
|
+ return false
|
|
|
|
|
|
- var my_items = [[5, "Potato"], [9, "Rice"], [4, "Tomato"]]
|
|
|
|
- my_items.sort_custom(MyCustomSorter.sort_ascending)
|
|
|
|
- print(my_items) # Prints [[4, Tomato], [5, Potato], [9, Rice]].
|
|
|
|
|
|
+ func _ready():
|
|
|
|
+ var my_items = [[5, "Potato"], [9, "Rice"], [4, "Tomato"]]
|
|
|
|
+ my_items.sort_custom(sort_ascending)
|
|
|
|
+ print(my_items) # Prints [[4, Tomato], [5, Potato], [9, Rice]].
|
|
|
|
+
|
|
|
|
+ # Descending, lambda version.
|
|
|
|
+ my_items.sort_custom(func(a, b): return a[0] > b[0])
|
|
|
|
+ print(my_items) # Prints [[9, Rice], [5, Potato], [4, Tomato]].
|
|
[/gdscript]
|
|
[/gdscript]
|
|
[csharp]
|
|
[csharp]
|
|
// There is no custom sort support for Godot.Collections.Array
|
|
// There is no custom sort support for Godot.Collections.Array
|