Give example of one-liner for Array natural sort

The documentation for the sort method warns the user that it doesn't do natural sort but fails to provide a solution when it's just a one liner thanks to String.naturalnocasecmp_to() and lambda support

This suggests exactly the same algorithm as used by the filesystem dock for file sorting.

Co-authored-by: Hugo Locurcio <hugo.locurcio@hugo.pro>
This commit is contained in:
Lisandro Lorea 2022-01-15 20:07:01 -03:00
parent 324dca57af
commit 176a9c738f
No known key found for this signature in database
GPG key ID: 2EADC564F354D2D7

View file

@ -470,6 +470,12 @@
// There is no sort support for Godot.Collections.Array // There is no sort support for Godot.Collections.Array
[/csharp] [/csharp]
[/codeblocks] [/codeblocks]
To perform natural order sorting, you can use [method sort_custom] with [method String.naturalnocasecmp_to] as follows:
[codeblock]
var strings = ["string1", "string2", "string10", "string11"]
strings.sort_custom(func(a, b): return a.naturalnocasecmp_to(b) &lt; 0)
print(strings) # Prints [string1, string2, string10, string11]
[/codeblock]
</description> </description>
</method> </method>
<method name="sort_custom"> <method name="sort_custom">