PHP | Ds\Deque sort()函数
Ds\Deque::sort()函数是PHP中的一个内置函数,用于通过按递增顺序排列元素来对 Deque 进行适当的排序。
句法:
public Ds\Deque::sort( $comparator ) : void
参数::此函数接受单个参数$comparator ,其中包含决定如何对元素进行排序的函数。它有助于自定义排序函数。
返回值:此函数不返回任何值。
下面的程序说明了PHP中的Ds\Deque::sort()函数:
方案一:
sort();
echo("Sorted Deque\n");
// Display the Deque elements
print_r($deck);;
?>
输出:
Elements of Deque
Ds\Deque Object
(
[0] => 5
[1] => 6
[2] => 3
[3] => 2
[4] => 7
[5] => 1
)
Sorted Deque
Ds\Deque Object
(
[0] => 1
[1] => 2
[2] => 3
[3] => 5
[4] => 6
[5] => 7
)
方案二:
sort(function($first, $second) {
return $first <= $second;
});
echo("Sorted Deque\n");
// Display the Deque elements
print_r($deck);;
?>
输出:
Elements of Deque
Ds\Deque Object
(
[0] => 5
[1] => 6
[2] => 3
[3] => 2
[4] => 7
[5] => 1
)
Sorted Deque
Ds\Deque Object
(
[0] => 7
[1] => 6
[2] => 5
[3] => 3
[4] => 2
[5] => 1
)
参考: http: PHP。 PHP