# FixUtil Documentation

Back to summary

`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).

## 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 `true` when the first element is smaller than the second). 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 sort_algorithm(array, off: Integer, len: Integer, data, cmp_expr, swap_code)`
  General sorting algorithm. This macro embeds a specialized version of sorting function using the provided comparison and swap code fragments. The `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.
  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

## 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 true when the first value is smaller than the second).
`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.