FixUtil Documentation

Back to summary

import "util/sort";

There are two variants of the sort algorithms. The general sorting algorithm is slower but preserves the original order of the equal values. It is preferred to use the quick sort variant whenever possible as it is much faster and doesn't allocate any extra memory.

The general sorting algorithm has an advantage when sorting by multiple different keys as it can be sorted repeatedly. However you can achieve the same result using the quick sort algorithm if you compare among all the keys in a single comparison.

You can use the provided macros for better performance (the comparison and swap code fragments will get inlined into a specialized sorting function).

Global macros

macro array_swap(array, idx1: Integer, idx2: Integer)
Swaps values in the array between given indicies.
macro array_sort(array, cmp_expr)
macro array_sort_range(array, off: Integer, len: Integer, cmp_expr)
Sorts the array using given comparison expression (must produce negative integer when the first element is smaller than the second, positive integer when the first element is bigger than the second, or zero when they are equal). You can optionally sort in the specified range only.

Variables available in the expressions:
array - reference to the array
index1 - index of the first element
index2 - index of the second element
value1 - value of the first element
value2 - value of the second element
macro array_quick_sort(array, cmp_expr)
macro array_quick_sort_range(array, off: Integer, len: Integer, cmp_expr)
A quick sort variant of the macros. The comparison expression must produce true when the first element is smaller than the second. The order among equal values is undefined.
macro sort_algorithm(array, off: Integer, len: Integer, data, cmp_expr, swap_code)
General sorting algorithm. This macro embeds a specialized version of the sorting function using the provided comparison and swap code fragments. The array and data are made available to the expressions. The comparison expression must produce negative integer when the first element is smaller than the second, positive integer when the first element is bigger than the second, or zero when they are equal.

Variables available in the expressions:
array - reference to the array (or any other kind of type)
data - reference to the extra passed data
index1 - index of the first element
index2 - index of the second element
macro quick_sort_algorithm(array, off: Integer, len: Integer, data, cmp_expr, swap_code)
A quick sort variant of the sort algorithm. The comparison expression must produce true when the first element is smaller than the second. The order among equal values is undefined.

Global functions

function array_sort_ints(array: Integer[])
function array_sort_ints(array: Integer[], off: Integer, len: Integer)
Sorts the integer values (optionally in the specified range only).
function array_sort_floats(array: Float[])
function array_sort_floats(array: Float[], off: Integer, len: Integer)
Sorts the float values (optionally in the specified range only).
function array_sort_strings(array: String[])
function array_sort_strings(array: String[], off: Integer, len: Integer)
Sorts the string values (optionally in the specified range only).
function array_sort_custom(array, cmp_func, data)
function array_sort_custom(array, off: Integer, len: Integer, cmp_func, data)
Sorts the values using custom comparison function (takes 3 parameters, first is data, then two values, must return negative integer when the first value is smaller than the second, positive integer when the first value is bigger than the second, or zero when they are equal).
function array_quick_sort_custom(array, cmp_func, data)
function array_quick_sort_custom(array, off: Integer, len: Integer, cmp_func, data)
A quick sort variant of the sort algorithm. The comparison function must return true when the first element is smaller than the second. The order among equal values is undefined.
function general_sort(array, cmp_func, swap_func, data)
function general_sort(array, off: Integer, len: Integer, cmp_func, swap_func, data)
General sorting algorithm. Sort the array (or any other kind of type) in the specified range using comparison and swap functions. Both functions take 4 parameters, first is data, second is the array and then the indicies for the first and second value. The comparison function must return negative integer when the first element is smaller than the second, positive integer when the first element is bigger than the second, or zero when they are equal.
function general_quick_sort(array, cmp_func, swap_func, data)
function general_quick_sort(array, off: Integer, len: Integer, cmp_func, swap_func, data)
A quick sort variant of the sort algorithm. The comparison function must return true when the first element is smaller than the second. The order among equal values is undefined.