PHP 8.3.4 Released!

Ds\Vector::sorted

(PECL ds >= 1.0.0)

Ds\Vector::sortedReturns a sorted copy

説明

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

Returns a sorted copy, using an optional comparator function.

パラメータ

comparator

比較関数は、最初の引数と二番目の引数の比較結果を返します。最初の引数のほうが二番目の引数より大きい場合は正の整数を、二番目の引数と等しい場合はゼロを、そして二番目の引数より小さい場合は負の整数を返す必要があります。

callback(mixed $a, mixed $b): int
警告

float のような 非整数 を比較関数が返すと、その返り値を内部的に int にキャストして使います。 つまり、0.990.1 といった値は整数値 0 にキャストされ、 値が等しいとみなされます。

戻り値

Returns a sorted copy of the vector.

例1 Ds\Vector::sorted() example

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

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

上の例の出力は、 たとえば以下のようになります。

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

例2 Ds\Vector::sorted() example using a comparator

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

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

print_r($sorted);
?>

上の例の出力は、 たとえば以下のようになります。

Ds\Vector Object
(
    [0] => 5
    [1] => 4
    [2] => 3
    [3] => 2
    [4] => 1
)
add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top