Arrays\sort()

transformer terminal accepts iterable returns Closure pure
int → (Iterable → T[])
At a glance — Terminal — fully materialises the source. Immutable alternative to PHP's in-place sort().

Creates a Closure that sorts an array or iterable using the standard PHP sort algorithm. Keys are NOT maintained.

/**
  * @param int $flag PHP sort flag constant.
  * @return Closure(iterable<int|string, mixed>):mixed[]
  */
Arrays\sort(int $flag = SORT_REGULAR): Closure

Returned Closure

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

$asc = Arrays\sort();
print_r($asc([3, 1, 2])); // [1, 2, 3]

Curried

This can be called inline using currying.

print_r(Arrays\sort(SORT_STRING)(['Zoo', 'cat', 'Dog']));
// ['Dog', 'Zoo', 'cat']

Details

Arrays Functions

Releated Array sort Functions