Syntax:void public DsVector::sort ($comparator)
Parameters: this function takes one parameter $comparator which is used to store the sorting function.Return value:This function does not return any value. The following programs illustrate the Ds / Vector::sort()function in PHP:Program 1:
// Declare a new vector
$vect
=
new
DsVector ([6, 5, 4, 3, 2, 1]);
echo
(
" Original vector "
);
// Show vector elements
print_r (
$vect
);
// Use the sort() function to sort
// vector elements
$vect
-> sort();
echo
(
" Sorted elements "
);
// Show sorted vector
// elements
print_r (
$vect
);
?>
Output:Original vector DsVector Object ([0] = > 6 [1] = > 5 [2] = > 4 [3] = > 3 [4] = > 2 [5] = > 1) Sorted elements DsVector Object ([0] = > 1 [1] = > 2 [2] = > 3 [3] = > 4 [4] = > 5 [5] = > 6)
Program 2:
// Declare a new vector
$vect
=
new
DsVector ([3, 6, 1, 2, 9, 7]);
echo
(
" Original vector "
);
// Show vector elements
print_r (
$vect
);
// Use the sort() function to sort
// vector elements
$vect
-> sort (
function
(
$element1
,
$element2
) {
return
$element2
< = >
$element1
;
});
echo
(
" Decreasing Sorted elements "
);
// Show sorted vector
// elements
print_r (
$vect
);
?>
Output:Original vector DsVector Object ([0] = > 3 [1] = > 6 [2] = > 1 [3] = > 2 [4] = > 9 [5] = > 7) Decreasing Sorted elements DsVector Object ([0] = > 9 [1] = > 7 [2] = > 6 [3] = > 3 [4] = > 2 [5] = > 1 )
Link: http://php.net/manual /en/ds-vector.sort.php