Ds \ Set::sorted()函数是PHP中的内置函数,用于返回给定集合的排序副本。
用法:
Ds\Set public Ds\Set::sorted ([ callable $comparator ])
参数:此函数接受比较器函数,根据该函数将在对Set进行排序时比较这些值。比较器应基于作为参数传递给它的两个值的比较返回以下值:
- 1:如果期望第一个元素小于第二个元素。
- -1:如果期望第一个元素大于第二个元素。
- 0:如果期望第一个元素等于第二个元素。
返回值:它还原给定集合的排序副本。
以下示例程序旨在说明PHP中的Ds \ Set::sorted()函数:
程序1:
<?php
// PHP program to illustrate sorted() function
$set = new \Ds\Set([20, 10, 30]);
// sort the Set
print_r($set->sorted());
?>
输出:
Ds\Set Object ( [0] => 10 [1] => 20 [2] => 30 )
程序2:
<?php
// Declare a new set
$set = new \Ds\Set([2, 3, 6, 5, 7, 1, 4]);
$sorted = $set->sorted(function($a, $b) {
return $b <=> $a;
});
print_r($sorted);
?>
输出:
Ds\Set Object ( [0] => 7 [1] => 6 [2] => 5 [3] => 4 [4] => 3 [5] => 2 [6] => 1 )
参考: https://www.php.net/manual/en/ds-set.sorted.php
相关用法
- PHP Ds\Map sorted()用法及代码示例
- PHP Ds\Vector sorted()用法及代码示例
- PHP Ds\Deque sorted()用法及代码示例
- PHP Ds\Sequence sorted()用法及代码示例
- p5.js sq()用法及代码示例
- PHP Ds\Set first()用法及代码示例
- PHP Ds\Set get()用法及代码示例
- PHP Ds\Map put()用法及代码示例
- p5.js abs()用法及代码示例
- PHP Ds\Set last()用法及代码示例
- PHP dir()用法及代码示例
注:本文由纯净天空筛选整理自R_Raj大神的英文原创作品 PHP | Ds\Set sorted() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。