PHP | Ds\Vector sort() Function

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:

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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);
  
?>

chevron_right


Output:

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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);
  
?>

chevron_right


Output:

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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.