CakeFest 2024: The Official CakePHP Conference

Ds\Deque::sorted

(PECL ds >= 1.0.0)

Ds\Deque::sortedReturns a sorted copy

説明

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

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 deque.

例1 Ds\Deque::sorted() example

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

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

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

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

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

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

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

print_r($sorted);
?>

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

Ds\Deque 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