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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
