Ds\Map::ksort
Ds\Map::ksort
(PECL ds >= 1.0.0)
Ds\Map::ksort — Sorts the map in-place by key.
Description
public void Ds\Map::ksort ([ callable $comparator ] )
Sorts the map in-place by key, using an optional comparator
function.
Parameters
-
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.
int callback ( mixed $a, mixed $b )
Return Values
No value is returned.
Examples
Example #1 Ds\Map::ksort() example
<?php $map = new \Ds\Map(["b" => 2, "c" => 3, "a" => 1]); $map->ksort(); print_r($map); ?>
The above example will output something similar to:
Ds\Map Object ( [0] => Ds\Pair Object ( [key] => a [value] => 1 ) [1] => Ds\Pair Object ( [key] => b [value] => 2 ) [2] => Ds\Pair Object ( [key] => c [value] => 3 ) )
Example #2 Ds\Map::ksort() example using a comparator
<?php $map = new \Ds\Map([1 => "x", 2 => "y", 0 => "z"]); // Reverse $map->ksort(function($a, $b) { return $b <=> $a; }); print_r($map); ?>
The above example will output something similar to:
Ds\Map Object Ds\Map Object ( [0] => Ds\Pair Object ( [key] => 2 [value] => y ) [1] => Ds\Pair Object ( [key] => 1 [value] => x ) [2] => Ds\Pair Object ( [key] => 0 [value] => z ) )
© 1997–2017 The PHP Documentation Group
Licensed under the Creative Commons Attribution License v3.0 or later.
https://secure.php.net/manual/en/ds-map.ksort.php