(PECL ds >= 1.0.0)
Ds\Map::sort — Sorts the map in-place by value
Sorts the map in-place by value, using an optional comparator
function.
comparator
The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second. Note that before PHP 7.0.0 this integer had to be in the range from -2147483648 to 2147483647.
No value is returned.
Example #1 Ds\Map::sort() example
<?php
$map = new \Ds\Map(["a" => 2, "b" => 3, "c" => 1]);
$map->sort();
print_r($map);
?>
The above example will output something similar to:
Ds\Map Object ( [0] => Ds\Pair Object ( [key] => c [value] => 1 ) [1] => Ds\Pair Object ( [key] => a [value] => 2 ) [2] => Ds\Pair Object ( [key] => b [value] => 3 ) )
Example #2 Ds\Map::sort() example using a comparator
<?php
$map = new \Ds\Map(["a" => 2, "b" => 3, "c" => 1]);
$map->sort(function($a, $b) {
return $b <=> $a;
});
print_r($map);
?>
The above example will output something similar to:
Ds\Map Object ( [0] => Ds\Pair Object ( [key] => b [value] => 3 ) [1] => Ds\Pair Object ( [key] => a [value] => 2 ) [2] => Ds\Pair Object ( [key] => c [value] => 1 ) )