array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'zh', ), 'this' => array ( 0 => 'ds-vector.sort.php', 1 => 'Ds\\Vector::sort', ), 'up' => array ( 0 => 'class.ds-vector.php', 1 => 'Ds\\Vector', ), 'prev' => array ( 0 => 'ds-vector.slice.php', 1 => 'Ds\\Vector::slice', ), 'next' => array ( 0 => 'ds-vector.sorted.php', 1 => 'Ds\\Vector::sorted', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'en', 'path' => 'reference/ds/ds/vector/sort.xml', ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); ?>
(PECL ds >= 1.0.0)
Ds\Vector::sort — Sorts the vector in-place
Sorts the vector in-place, using an optional comparator
function.
comparator
在第一个参数小于,等于或大于第二个参数时,该比较函数必须相应地返回一个小于,等于或大于 0 的整数。
没有返回值。
示例 #1 Ds\Vector::sort() example
<?php
$vector = new \Ds\Vector([4, 5, 1, 3, 2]);
$vector->sort();
print_r($vector);
?>
以上示例的输出类似于:
Ds\Vector Object ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )
示例 #2 Ds\Vector::sort() example using a comparator
<?php
$vector = new \Ds\Vector([4, 5, 1, 3, 2]);
$vector->sort(function($a, $b) {
return $b <=> $a;
});
print_r($vector);
?>
以上示例的输出类似于:
Ds\Vector Object ( [0] => 5 [1] => 4 [2] => 3 [3] => 2 [4] => 1 )