Arrays\uksort()

higher-order transformer terminal accepts iterable returns Closure pure
((int|string, int|string) → int) → (Iterable → T[])
At a glance — Custom key sort — comparator returns negative / zero / positive like any standard comparator. Keys preserved. Terminal.

Creates a Closure that sorts an array or iterable by key using a custom comparator function.

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

Returned Closure

When Arrays\uksort() 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.

$byKeyLen = Arrays\uksort(fn($a, $b) => strlen($a) - strlen($b));

print_r($byKeyLen(['bbb' => 1, 'a' => 2, 'cc' => 3]));
// ['a' => 2, 'cc' => 3, 'bbb' => 1]

Details

Arrays Functions

Releated Array sort Functions