PHP | Ds\Vector sort() Function
Last Updated :
22 Aug, 2019
Improve
The Ds\Vector::sort() function is an inbuilt function in PHP which is used to sort the elements of vector in-place. This will arrange the vector elements in increasing order.
Syntax:
PHP
Output:
PHP
Output:
void public Ds\Vector::sort( $comparator )Parameters: This function accepts a single parameter $comparator which is used to hold the sort function. Return Value: This function does not returns any value. Below programs illustrate the Ds\Vector::sort() function in PHP: Program 1:
<?php
// Declare new Vector
$vect = new \Ds\Vector([6, 5, 4, 3, 2, 1]);
echo("Original vector\n");
// Display the vector elements
print_r($vect);
// Use sort() function to sort
// the vector elements
$vect->sort();
echo("\nSorted elements\n");
// Display the sorted vector
// elements
print_r($vect);
?>
Original vector Ds\Vector Object ( [0] => 6 [1] => 5 [2] => 4 [3] => 3 [4] => 2 [5] => 1 ) Sorted elements Ds\Vector Object ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 )Program 2:
<?php
// Declare new Vector
$vect = new \Ds\Vector([3, 6, 1, 2, 9, 7]);
echo("Original vector\n");
// Display the vector elements
print_r($vect);
// Use sort() function to sort
// the vector elements
$vect->sort(function($element1, $element2) {
return $element2 <=> $element1;
});
echo("\nDecreasing Sorted elements\n");
// Display the sorted vector
// elements
print_r($vect);
?>
Original vector Ds\Vector Object ( [0] => 3 [1] => 6 [2] => 1 [3] => 2 [4] => 9 [5] => 7 ) Decreasing Sorted elements Ds\Vector Object ( [0] => 9 [1] => 7 [2] => 6 [3] => 3 [4] => 2 [5] => 1 )Reference: http://php.net/manual/en/ds-vector.sort.php