Skip to content

System.Sort

Dennis G. edited this page Jun 9, 2026 · 3 revisions

Declaration

function Sort(Values: array, Sorter: function): array = \built-in\

Description

The Sort function in the System package sorts an existing array using a comparer function.

The second parameter is a function accepting two parameters and returning a signed integer number. Its result should be depending on the two parameters passed:

  • -1 or any other negative number if the first argument is less than the second
  • 0 if the first and second argument are equal (or equivalent)
  • 1 or any positive number if the first argument is greater than the second

Because the function uses the Quicksort algorithm, the average call produces O(n * Log(n)) comparisons, the actual amount depending on the input array.

Remarks

  • If the length of Values is 0, Sorter is never called and an empty array ([]) or record ({}) is returned.
  • If the length of Values is 1, Sorter is never called and the input array is returned without modifications.
  • If Sorter demands more or less arguments than 2, an exception is raised.

See also

Clone this wiki locally