|
706 | 706 | GD.Print(numbers); // Prints [2.5, 5, 8, 10]
|
707 | 707 | [/csharp]
|
708 | 708 | [/codeblocks]
|
709 |
| - [b]Note:[/b] The sorting algorithm used is not [url=https://en.wikipedia.org/wiki/Sorting_algorithm#Stability]stable[/url]. This means that equivalent elements (such as [code]2[/code] and [code]2.0[/code]) may have their order changed when calling [method sort]. |
| 709 | + [b]Note:[/b] The sorting algorithm used is not [url=https://en.wikipedia.org/wiki/Sorting_algorithm#Stability]stable[/url]. This means that equivalent elements (such as [code]2[/code] and [code]2.0[/code]) may have their order changed when calling [method sort]. See also [method sort_stable] |
710 | 710 | </description>
|
711 | 711 | </method>
|
712 | 712 | <method name="sort_custom">
|
|
737 | 737 | print(files) # Prints ["newfile1", "newfile2", "newfile10", "newfile11"]
|
738 | 738 | [/codeblock]
|
739 | 739 | [b]Note:[/b] In C#, this method is not supported.
|
740 |
| - [b]Note:[/b] The sorting algorithm used is not [url=https://en.wikipedia.org/wiki/Sorting_algorithm#Stability]stable[/url]. This means that values considered equal may have their order changed when calling this method. |
| 740 | + [b]Note:[/b] The sorting algorithm used is not [url=https://en.wikipedia.org/wiki/Sorting_algorithm#Stability]stable[/url]. This means that values considered equal may have their order changed when calling this method. See also [method sort_custom_stable] |
741 | 741 | [b]Note:[/b] You should not randomize the return value of [param func], as the heapsort algorithm expects a consistent result. Randomizing the return value will result in unexpected behavior.
|
742 | 742 | </description>
|
743 | 743 | </method>
|
| 744 | + <method name="sort_custom_stable"> |
| 745 | + <return type="void" /> |
| 746 | + <param index="0" name="func" type="Callable" /> |
| 747 | + <description> |
| 748 | + Sorts the array using a [url=https://en.wikipedia.org/wiki/Sorting_algorithm#Stability]stable[/url] sort algorithm with a custom [Callable]. Equal elements remain in the same order after sorting. |
| 749 | + [param func] is called as many times as necessary, receiving two array elements as arguments. The function should return [code]true[/code] if the first element should be moved [i]behind[/i] the second one, otherwise it should return [code]false[/code]. |
| 750 | + [codeblock] |
| 751 | + func sort_ascending(a, b): |
| 752 | + if a[1] < b[1]: |
| 753 | + return true |
| 754 | + return false |
| 755 | + |
| 756 | + func _ready(): |
| 757 | + var my_items = [["Tomato", 5], ["Apple", 9], ["Rice", 4], ["Orange", 9]] |
| 758 | + my_items.sort_custom_stable(sort_ascending) |
| 759 | + print(my_items) # Prints [["Rice", 4], ["Tomato", 5], ["Apple", 9], ["Orange", 9]] |
| 760 | + |
| 761 | + # Sort descending, using a lambda function. |
| 762 | + my_items.sort_custom_stable(func(a, b): return a[1] > b[1]) |
| 763 | + print(my_items) # Prints [["Apple", 9], ["Orange", 9], ["Tomato", 5], ["Rice", 4]] |
| 764 | + [/codeblock] |
| 765 | + It may also be necessary to use this method to sort strings by natural order, with [method String.naturalnocasecmp_to], as in the following example: |
| 766 | + [codeblock] |
| 767 | + var files = ["newfile1", "newfile2", "newfile10", "newfile11"] |
| 768 | + files.sort_custom_stable(func(a, b): return a.naturalnocasecmp_to(b) < 0) |
| 769 | + print(files) # Prints ["newfile1", "newfile2", "newfile10", "newfile11"] |
| 770 | + [/codeblock] |
| 771 | + [b]Note:[/b] In C#, this method is not supported. |
| 772 | + </description> |
| 773 | + </method> |
| 774 | + <method name="sort_stable"> |
| 775 | + <return type="void" /> |
| 776 | + <description> |
| 777 | + Sorts the array in ascending order using a [url=https://en.wikipedia.org/wiki/Sorting_algorithm#Stability]stable[/url] sort algorithm. The final order is dependent on the "less than" ([code]<[/code]) comparison between elements. Equal elements remain in the same order after sorting. |
| 778 | + [codeblocks] |
| 779 | + [gdscript] |
| 780 | + var numbers = [10, 5, 2.5, 8, 8.0] |
| 781 | + numbers.sort_stable() |
| 782 | + print(numbers) # Prints [2.5, 5, 8, 8.0, 10] |
| 783 | + [/gdscript] |
| 784 | + [csharp] |
| 785 | + var numbers = new Godot.Collections.Array { 10, 5, 2.5, 8, 8.0 }; |
| 786 | + numbers.SortStable(); |
| 787 | + GD.Print(numbers); // Prints [2.5, 5, 8, 8.0, 10] |
| 788 | + [/csharp] |
| 789 | + [/codeblocks] |
| 790 | + </description> |
| 791 | + </method> |
744 | 792 | </methods>
|
745 | 793 | <operators>
|
746 | 794 | <operator name="operator !=">
|
|
0 commit comments