Quirrel4.53.0

types.Array.sort

Method on every array
array.sort([comparator: function], ...): any

Sorts the array in place.

The binding carries no declaration string, so the VM cannot report parameter names. The names above are from this page; the types and attributes still come from the VM. The VM also cannot tell an optional parameter from a variadic tail here, so read the brackets and any trailing ... from the prose below, not from the signature.

Parameters

comparatorfunctioncompares two elements; the default ordering when left out (see Notes) optional
...any repeats

Return value

This array.

Errors

Whatever comparator throws. Without a comparator, throws comparison between X and Y for a pair of elements neither numeric-with-numeric, string-with-string, nor both instances of a class that defines _cmp.

Notes

comparator(a, b) must return a negative number when a sorts before b, a positive number when it sorts after, and 0 when they are equal, the same convention as _cmp.

Despite the trailing ... in the signature above, sort is not variadic: it takes zero or one real argument. Calling it with more than one extra argument does not throw, unlike findvalue; it silently falls back to the default ordering, as if comparator had been left out entirely.

Without a comparator, elements are compared the way < compares them: numbers by value, strings lexicographically, and an instance through its _cmp metamethod if it has one; anything else is a comparison between error.

Each call of a script comparator costs much more than a compare without one. For a descending order, sort_reversed gives the result of arr.sort(@(a, b) b <=> a) with no comparator call, 6 to 7 times faster for 32 elements or more. For an order by a value of each element, such as a field, sortby and sortby_reversed call a script function n times in place of about n log2(n) times.

sort is stable: elements that compare equal keep their relative order. It is a merge sort: the first merge that moves elements allocates a buffer of at most half the array (n/2 slots) for the time of the call; an array of 32 elements or less, or one already sorted, needs none. An already sorted array costs n - 1 compares, a reversed one about n + n/32.

A comparator that does not define a consistent order (one that returns different signs for the same pair) gives some permutation of the array; no error is raised for it.

Example

examples/types/array/sort.nut
let a = [3, 1, 2];
a.sort();                          // no comparator: default ascending order
println("a sorted =", ", ".join(a.map(@(v) v.tostring())));

let b = [3, 1, 2];
b.sort(function(x, y) { return y <=> x });   // one comparator: descending order
println("b sorted desc =", ", ".join(b.map(@(v) v.tostring())));

// two extra arguments: the comparator is silently ignored, default order wins
let c = [3, 1, 2];
c.sort(function(x, y) { return y <=> x }, "ignored");
println("c sorted (comparator ignored) =", ", ".join(c.map(@(v) v.tostring())));
Output:
a sorted = 1, 2, 3
b sorted desc = 3, 2, 1
c sorted (comparator ignored) = 1, 2, 3

See also

sort_reversedSorts the array in place in descending order: the order of sort with the two arguments of each compare swapped.
sortbySorts the array in place by the value that key returns for each element.
reverseReverses the array in place.
arrayclass index