Skip to content

Core\Arr::sort

Core\Arr::sort(array<T> $a, {…}): array<T>Core\Arr::sort(
    array<T> $a,
    {…},
): array<T>

Returns the values of an array in order, from the smallest to the largest.

The keys are renumbered from 0, so the result is a plain list. Write { preserveKeys: true } to keep every value under its own key. Write { order: Core\Arr::Order::Desc } for the largest value first. Values that count as equal keep the order they were added in.

Two numbers are compared as numbers. Two strings are compared character by character, so "10" comes before "9". A number and a string have no order between them, and an array holding both throws an error. Write { by: ... } with a function that returns the value to sort on. Write { comparator: ... } with a function of two values for any other order. If the function in by or comparator throws an error, the sort stops and throws the same error.

The examples below show the values in order and the largest first, a sort by something you compute, and a table sorted with its keys kept.

Parameters

  • $aarray<T>The array to sort.

Options

  • bycallableoptionalCalled once per entry as ($value, $key), and may declare fewer parameters. Its result is compared instead of the entry.
  • orderCore\Arr::OrderoptionalCore\Arr::Order::Asc (the default) or Core\Arr::Order::Desc. Desc reverses the comparison, so equal entries keep their relative order.
  • comparatorcallableoptionalCalled as ($a, $b) on two values, or on two results of by. It returns a negative number, zero or a positive number. It replaces the natural order.
  • preserveKeysbooloptionalKeeps the key of each entry. By default, the result has new keys from "0".

Return value

Returns array<T>. The sorted entries. A natural order exists only between two numbers, two strings (compared byte by byte), two bools or two nulls. "10" sorts before "9".

Errors

  • RuntimeErrorTwo entries have no natural order and no comparator was given. An int and a string have no natural order, and neither does any object. The error is also thrown when the comparator returns NaN.

Examples

Values and order

<?nvs
array<int> $numbers = [3, 1, 4, 1, 5, 9, 2, 6];
var $sorted = Core\Arr::sort($numbers);
foreach ($sorted as int $n) {
echo $n, ' ';
}
echo "\n";
var $reversed = Core\Arr::sort($numbers, { order: Core\Arr::Order::Desc });
foreach ($reversed as int $n) {
echo $n, ' ';
}
echo "\n";
Output
1 1 2 3 4 5 6 9
9 6 5 4 3 2 1 1

Sort by key extractor

<?nvs
// `{by:}` takes a function that returns the key to sort by. Here the key is the
// length of each word, so this prints `fig` first, then `banana` and `cherry`.
array<string> $words = ['banana', 'fig', 'cherry'];
var $byLength = Core\Arr::sort($words, { by: fn(string $w): uint => Core\Str::length($w) });
foreach ($byLength as string $w) {
echo $w, "\n";
}
Output
fig
banana
cherry

The highest score first

<?nvs
// Shows how to sort a table and keep its keys. A page does this to show a
// scoreboard with the best result at the top.
array<uint> $points = ['bo' => 12, 'ada' => 31, 'cy' => 7];
// `preserveKeys` keeps every value under its own key. `Core\Arr::Order::Desc` puts
// the largest value first.
array<uint> $ranked = Core\Arr::sort($points, { order: Core\Arr::Order::Desc, preserveKeys: true });
// This prints ada: 31, then bo: 12, then cy: 7.
foreach ($ranked as string $name => uint $score) {
echo $name, ': ', $score, "\n";
}
Output
ada: 31
bo: 12
cy: 7