CakeFest 2024: The Official CakePHP Conference

Ds\Set::sort

(PECL ds >= 1.0.0)

Ds\Set::sort Sorts the set in-place

説明

public Ds\Set::sort(callable $comparator = ?): void

Sorts the set in-place, using an optional comparator function.

パラメータ

comparator

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

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

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

戻り値

値を返しません。

例1 Ds\Set::sort() example

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

print_r($set);
?>

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

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

例2 Ds\Set::sort() example using a comparator

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

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

print_r($set);
?>

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

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