sort()
The sort() method sort an array by values using a function or default sort function
Example.php
<?php use kim\present\utils\arrays\ArrayUtils;
$arrayUtils = ArrayUtils::from(["orange", "banana", "apple", "raspberry", "kiwi"]);
$arrayUtils->sort();
// expected output: ["apple", "banana", "kiwi", "orange", "raspberry"]
$arrayUtils->sort(function($a, $b){ return strcmp($a, $b) * -1; })
// expected output: ["raspberry", "orange", "kiwi", "banana", "apple"]
$arrayUtils->sort(?callable $callback = null) : ArrayUtils;
$callback
A function to compare element for sort, taking two arguments:$a
The comparison target A$b
The comparison target B
Default isNULL
, If is null, Sort by default sort function.
- A sorted array.
$arrayUtils->sortAs(?callable $callback = null) : array;
ArrayUtils::sortFrom(iterable $from, ?callable $callback = null) : ArrayUtils;
ArrayUtils::sortFromAs(iterable $from, ?callable $callback = null) : array;
Last modified 2yr ago