(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比較関数は、最初の引数と二番目の引数の比較結果を返します。最初の引数のほうが二番目の引数より大きい場合は正の整数を、二番目の引数と等しい場合はゼロを、そして二番目の引数より小さい場合は負の整数を返す必要があります。
値を返しません。
例1 Ds\Map::sort() example
<?php
$map = new \Ds\Map(["a" => 2, "b" => 3, "c" => 1]);
$map->sort();
print_r($map);
?>上の例の出力は、 たとえば以下のようになります。
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
)
)
例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);
?>上の例の出力は、 たとえば以下のようになります。
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
)
)