import "util/sort";
The sorting is using the quick sort algorithm. This is an unstable sorting algorithm which means that values compared as the same have arbitrary order in the resulting array. The upside is that there are no extra allocations. This property means that sorting repeatedly using different sorting keys results in a bad behavior. This is easily solved by merging the different sorting comparisons into one and just doing a single sorting operation.
You can use the provided macros for better performance (the comparison and swap code fragments will get inlined into a specialized sorting function).
macro array_swap(array, idx1: Integer, idx2: Integer)
macro array_sort(array, cmp_expr)
macro array_sort_range(array, off: Integer, len: Integer, cmp_expr)
true
when the
first element is smaller than the second). You can optionally sort in the specified range only.array
- reference to the arrayindex1
- index of the first elementindex2
- index of the second elementvalue1
- value of the first elementvalue2
- value of the second elementmacro sort_algorithm(array, off: Integer, len: Integer, data, cmp_expr, swap_code)
array
and
data
are made available to the expressions. The comparsion expression
must have true
as a result when the first element is smaller than the second.array
- reference to the array (or any other kind of type)data
- reference to the extra passed dataindex1
- index of the first elementindex2
- index of the second element
function array_sort_ints(array: Integer[])
function array_sort_ints(array: Integer[], off: Integer, len: Integer)
function array_sort_floats(array: Float[])
function array_sort_floats(array: Float[], off: Integer, len: Integer)
function array_sort_strings(array: String[])
function array_sort_strings(array: String[], off: Integer, len: Integer)
function array_sort_custom(array, cmp_func, data)
function array_sort_custom(array, off: Integer, len: Integer, cmp_func, data)
function general_sort(array, cmp_func, swap_func, data)
function general_sort(array, off: Integer, len: Integer, cmp_func, swap_func, data)