Ds \ Vector::sorted()函數是PHP中的一個內置函數,用於通過創建原始向量的副本來對向量的元素進行排序。這將使用默認比較器按遞增順序排列矢量元素。
用法:
Ds\Vector public Ds\Vector::sorted( $comparator )
參數:該函數接受具有排序函數的單個參數$comparator。
返回值:此函數返回已排序向量的副本。
以下示例程序旨在說明PHP中的Ds \ Vector::sorted()函數:
程序1:
<?php
// Declare new Vector
$vect = new \Ds\Vector([6, 5, 4, 3, 2, 1]);
echo("Original vector\n");
// Display the vector elements
var_dump($vect);
// Use sorted() function to sort
// the copy of vector elements
$res = $vect->sorted();
echo("\nSorted elements\n");
// Display the sorted elements
var_dump($res);
?>輸出:
Original vector
object(Ds\Vector)#1 (6) {
[0]=>
int(6)
[1]=>
int(5)
[2]=>
int(4)
[3]=>
int(3)
[4]=>
int(2)
[5]=>
int(1)
}
Sorted elements
object(Ds\Vector)#2 (6) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
[3]=>
int(4)
[4]=>
int(5)
[5]=>
int(6)
}
程序2:
<?php
// Declare new Vector
$vect = new \Ds\Vector([3, 6, 1, 2, 9, 7]);
echo("Original vector\n");
// Display the vector elements
var_dump($vect);
// Use sorted() function to sort
// the copy of vector elements
$res = $arr->sorted(function($element1, $element2) {
return $element1 <=> $element2;
});
echo("\nSorted elements\n");
// Display the sorted elements
var_dump($res);
?>輸出:
Original vector
object(Ds\Vector)#1 (6) {
[0]=>
int(3)
[1]=>
int(6)
[2]=>
int(1)
[3]=>
int(2)
[4]=>
int(9)
[5]=>
int(7)
}
Sorted elements
object(Ds\Vector)#3 (6) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
[3]=>
int(6)
[4]=>
int(7)
[5]=>
int(9)
}
參考: http://php.net/manual/en/ds-vector.sorted.php
相關用法
- PHP Ds\Set sorted()用法及代碼示例
- PHP Ds\Map sorted()用法及代碼示例
- PHP Ds\Sequence sorted()用法及代碼示例
- PHP Ds\Deque sorted()用法及代碼示例
- d3.js d3.min()用法及代碼示例
- PHP Ds\Set add()用法及代碼示例
- CSS rgb()用法及代碼示例
- PHP Ds\Set contains()用法及代碼示例
- PHP Ds\Set last()用法及代碼示例
- PHP Ds\Set first()用法及代碼示例
- PHP each()用法及代碼示例
注:本文由純淨天空篩選整理自barykrg大神的英文原創作品 PHP | Ds\Vector sorted() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
