Arrays\uasort()

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

Creates a Closure that sorts an array or iterable by value using a custom comparator, preserving keys.

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

Returned Closure

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

$byAuthor = Arrays\uasort(fn($a, $b) => strcmp($a['author'], $b['author']));

$books = [
  'bk1' => ['author' => 'Carter'],
  'bk2' => ['author' => 'Adams'],
  'bk3' => ['author' => 'Baker'],
];

print_r($byAuthor($books));
// ['bk2' => ..., 'bk3' => ..., 'bk1' => ...]

Details

Arrays Functions

Releated Array sort Functions