Sort array

Purpose: Sort array.

sort-array <array> [ algorithm <algorithm> ] [ descending ]

sort-array will sort <array>, which was created with new-array and must of type "string", "number" or "double". By default the "quick_sort" algorithm is used.
Algorithm
You can specify the algorithm to use with <algorithm> (in "algorithm" clause), which can be a string constant:
Each algorithm has its own strengths, weaknesses and use cases, but in general the fastest are "quick_sort", "tim_sort", "heap_sort", "merge_sort_in_place" etc. For instance, "tim_sort" is generally guaranteed to have complexity of less than O(N log(N) and in the best case O(N), however it would generally use more memory than quick sort. You can experiment which algorithm best suites your real-world data, as actual performance may vary.
Order
By default, sorting is in the ascending order. To produce a descending sort, use "descending" clause.
Examples
The array here will have values of "Z", D" and "A"; the sorted array will be "A", "D" and "Z":
new-array arr

write-array arr key 0 value "Z"
write-array arr key 1 value "D"
write-array arr key 2 value "A"

sort-array arr algorithm "tim_sort"

print-out arr[0], " ", arr[1], " ", arr[2] new-line

See also
Array
new-array  
purge-array  
read-array  
sort-array  
write-array  
See all
documentation


Copyright (c) 2019-2025 Gliim LLC. All contents on this web site is "AS IS" without warranties or guarantees of any kind.