(PECL ds >= 1.0.0)
Ds\Set::sorted — 回傳排序後的副本
comparator
比較函式必須回傳一個小於、等於或大於零的整數,如果第一個參數分別被認為小於、等於或大於第二個參數。
返回集合的已排序副本。
範例 #1 Ds\Set::sorted() 範例
<?php
$set = new \Ds\Set([4, 5, 1, 3, 2]);
print_r($set->sorted());
?>
上述範例將輸出類似以下內容
Ds\Set Object ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )
範例 #2 Ds\Set::sorted() 使用比較器的範例
<?php
$set = new \Ds\Set([4, 5, 1, 3, 2]);
$sorted = $set->sorted(function($a, $b) {
return $b <=> $a;
});
print_r($sorted);
?>
上述範例將輸出類似以下內容
Ds\Set Object ( [0] => 5 [1] => 4 [2] => 3 [3] => 2 [4] => 1 )