Arrays\usort()

higher-order transformer terminal accepts iterable returns Closure pure
((T, T) → int) → (Iterable → T[])
At a glance — Custom value sort with re-indexed output. Terminal.

Creates a Closure that sorts an array or iterable by value using a custom comparator. Keys are NOT maintained.

/**
  * @param callable(mixed $a, mixed $b): int $function
  * @return Closure(iterable<int|string, mixed>):mixed[]
  */
Arrays\usort(callable $function): Closure

Returned Closure

When Arrays\usort() is called, it returns the following Closure which can be used like a regular function.

/**
  * @param iterable<int|string, mixed> $source
  * @return mixed[]
  */
$function (iterable $source): array

Examples

Partial Application

This can be used to create a simple closure which can be used as a regular function.

$byLen = Arrays\usort(fn($a, $b) => strlen($a) - strlen($b));

print_r($byLen(['banana', 'a', 'to']));
// ['a', 'to', 'banana']

Details

Arrays Functions

Releated Array sort Functions