Ds \ Sequence::sort()函數是PHP中的內置函數,用於在同一位置對序列元素進行排序。
用法:
void abstract public Ds\Sequence::sort ([ callable $comparator ] )
參數:該函數接受單個參數$comparator,該參數用於保存比較函數。比較函數返回一個小於,大於或等於零的整數值。
返回值:此函數不返回任何值。
以下示例程序旨在說明PHP中的Ds \ Sequence::sort()函數:
程序1:
<?php
// Create new sequence
$seq = new \Ds\Vector([2, 4, 1, 9, 6, 5, 12, 9]);
// Use sort() function to sort
// the sequence element
$seq->sort();
print_r($seq);
?>輸出:
Ds\Vector Object
(
[0] => 1
[1] => 2
[2] => 4
[3] => 5
[4] => 6
[5] => 9
[6] => 9
[7] => 12
)
程序2:
<?php
// Create new sequence
$seq = new \Ds\Vector([2, 4, 1, 9, 6, 5, 12, 9]);
// Use sort() function to sort
// the sequence element
$seq->sort(function($x, $y) {
return $y <=> $x;
});
print_r($seq);
?>輸出:
Ds\Vector Object
(
[0] => 12
[1] => 9
[2] => 9
[3] => 6
[4] => 5
[5] => 4
[6] => 2
[7] => 1
)
參考: http://php.net/manual/en/ds-sequence.sort.php
相關用法
- p5.js sort()用法及代碼示例
- PHP Ds\Map sort()用法及代碼示例
- PHP Ds\Set sort()用法及代碼示例
- PHP sort()用法及代碼示例
- PHP Ds\Deque sort()用法及代碼示例
- Node.js sort()用法及代碼示例
- PHP Ds\Vector sort()用法及代碼示例
注:本文由純淨天空篩選整理自Mahadev99大神的英文原創作品 PHP | Ds\Sequence sort() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
