浏览代码

Merge pull request #14432 from rminderhoud/sort_custom

Added example for Array sort_custom function

[ci skip]
Rémi Verschelde 7 年之前
父节点
当前提交
43300bdcc2
共有 1 个文件被更改,包括 10 次插入0 次删除
  1. 10 0
      doc/classes/Array.xml

+ 10 - 0
doc/classes/Array.xml

@@ -284,6 +284,16 @@
 			</argument>
 			<description>
 				Sort the array using a custom method and return reference to the array. 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 true if the first argument is less than the second, and return false otherwise. Note: you cannot randomize the return value as the heapsort algorithm expects a deterministic result. Doing so will result in unexpected behavior.
+				[codeblock]
+				class MyCustomSorter:
+				    static func sort(a, b):
+				        if a[0] &lt; b[0]:
+				            return true
+				    return false
+
+				var my_items = [[5, "Potato"], [9, "Rice"], [4, "Tomato"]]
+				my_items.sort_custom(MyCustomSorter, "sort")
+				[/codeblock]
 			</description>
 		</method>
 	</methods>