phpday 2027 - Call for Papers

Ds\Set::sorted

(PECL ds >= 1.0.0)

Ds\Set::sorted — Returns a sorted copy

Descrizione

public function Ds\Set::sorted(callable $comparator = ?): Ds\Set

Returns a sorted copy, using an optional comparator function.

Elenco dei parametri

comparator

La funzione di confronto deve restituire un intero minore, uguale o maggiore di zero se il primo argomento è considerato rispettivamente minore, uguale o maggiore del secondo.

function callback(mixed $a, mixed $b): int
Attenzione

Restituire valori non interi dalla funzione di confronto, come float, comporterà un cast interno a int del valore restituito dal callback. Quindi valori come 0.99 e 0.1 verranno entrambi convertiti nel valore intero 0, e tali valori risulteranno uguali nel confronto.

Valori restituiti

Returns a sorted copy of the set.

Esempi

Example #1 Ds\Set::sorted() example

<?php
$set = new \Ds\Set([4, 5, 1, 3, 2]);

print_r($set->sorted());
?>

Il precedente esempio visualizzerà qualcosa di simile a:

Ds\Set Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)

Example #2 Ds\Set::sorted() example using a comparator

<?php
$set = new \Ds\Set([4, 5, 1, 3, 2]);

$sorted = $set->sorted(function($a, $b) {
    return $b <=> $a;
});

print_r($sorted);
?>

Il precedente esempio visualizzerà qualcosa di simile a:

Ds\Set Object
(
    [0] => 5
    [1] => 4
    [2] => 3
    [3] => 2
    [4] => 1
)

User Contributed Notes

There are no user contributed notes for this page.
Adding new notes is disabled. Please see the links in the Found A Problem? section.
To Top