array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'de', ), 'this' => array ( 0 => 'ds-map.ksort.php', 1 => 'Ds\\Map::ksort', 2 => 'Sorts the map in-place by key', ), 'up' => array ( 0 => 'class.ds-map.php', 1 => 'Ds\\Map', ), 'prev' => array ( 0 => 'ds-map.keys.php', 1 => 'Ds\\Map::keys', ), 'next' => array ( 0 => 'ds-map.ksorted.php', 1 => 'Ds\\Map::ksorted', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'en', 'path' => 'reference/ds/ds/map/ksort.xml', ), 'history' => array ( ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); contributors($setup); ?>

Ds\Map::ksort

(PECL ds >= 1.0.0)

Ds\Map::ksort Sorts the map in-place by key

Beschreibung

public Ds\Map::ksort(callable $comparator = ?): void

Sorts the map in-place by key, using an optional comparator function.

Parameter-Liste

comparator

Die Vergleichsfunktion muss einen Integer kleiner als, gleich oder größer als Null zurückgeben, wenn das erste Argument respektive kleiner, gleich oder größer als das zweite ist.

callback(mixed $a, mixed $b): int
Achtung

Wenn die Vergleichsfunktion nicht-ganzzahlige Werte zurückgibt, z. B. vom Typ float, wird der Rückgabewert des Callbacks intern in den Typ int umgewandelt. Werte wie 0.99 und 0.1 werden also beide in einen Integer-Wert von 0 umgewandelt, wodurch diese Werte als gleichwertig eingestuft werden.

Rückgabewerte

Es wird kein Wert zurückgegeben.

Beispiele

Beispiel #1 Ds\Map::ksort() example

<?php
$map
= new \Ds\Map(["b" => 2, "c" => 3, "a" => 1]);
$map->ksort();

print_r($map);
?>

Das oben gezeigte Beispiel erzeugt eine ähnliche Ausgabe wie:

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
        )

)

Beispiel #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);
?>

Das oben gezeigte Beispiel erzeugt eine ähnliche Ausgabe wie:

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
        )

)