sort_custom description and example updated

This commit is contained in:
cajallen 2020-01-10 12:24:49 -06:00
parent 4b3123104c
commit 05bbbb1225
1 changed files with 4 additions and 3 deletions

View File

@ -331,17 +331,18 @@
<argument index="1" name="func" type="String"> <argument index="1" name="func" type="String">
</argument> </argument>
<description> <description>
Sorts the array using a custom method. The arguments are an object that holds the method and the name of such method. The custom method receives two arguments (a pair of elements from the array) and must return [code]true[/code] if the first argument is less than the second, and return [code]false[/code] otherwise. Sorts the array using a custom method. The arguments are an object that holds the method and the name of such method. The custom method receives two arguments (a pair of elements from the array) and must return either [code]true[/code] or [code]false[/code].
[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.
[codeblock] [codeblock]
class MyCustomSorter: class MyCustomSorter:
static func sort(a, b): static func sort_ascending(a, b):
if a[0] &lt; b[0]: if a[0] &lt; b[0]:
return true return true
return false return false
var my_items = [[5, "Potato"], [9, "Rice"], [4, "Tomato"]] var my_items = [[5, "Potato"], [9, "Rice"], [4, "Tomato"]]
my_items.sort_custom(MyCustomSorter, "sort") my_items.sort_custom(MyCustomSorter, "sort_ascending")
print(my_items) # Prints [[4, Tomato], [5, Potato], [9, Rice]]
[/codeblock] [/codeblock]
</description> </description>
</method> </method>