HH\Lib\Dict\sort
Returns a new dict sorted by the values of the given KeyedTraversable
namespace HH\Lib\Dict;
function sort<Tk as arraykey, Tv>(
KeyedTraversable<Tk, Tv> $traversable,
?(function(Tv, Tv): num) $value_comparator = NULL,
): dict<Tk, Tv>;
If the optional comparator function isn't provided, the values will be sorted in ascending order.
- To sort by some computable property of each value, see
Dict\sort_by()
. - To sort by the keys of the KeyedTraversable, see
Dict\sort_by_key()
.
Time complexity: O((n log n) * c), where c is the complexity of the comparator function (which is O(1) if not provided explicitly) Space complexity: O(n)
Parameters
KeyedTraversable<Tk,
Tv> $traversable
?(function(Tv, Tv): num) $value_comparator = NULL
Returns
dict<Tk, Tv>
Examples
$result = Dict\sort(dict[1 => 2000, 2 => 1000, 3 => 9]);
print_r($result);
//result: dict[3=>9, 2=>1000, 1=>2000]
$result = Dict\sort(dict[]);
print_r($result);
//result: dict[]