PHP | Ds\Sequence sorted() Function

The Ds\Sequence::sorted() function is an inbuilt function in PHP which is used to return the sorted copy of sequence element.

Syntax:

Ds\Sequence abstract public Ds\Sequence::sorted( $comparator )

Parameters: This function accepts a single parameter $comparator which holds the comparison function. The comparison function returns an integer value which is less then or greater then or equal to zero.



Return value: This function returns the sorted copy of sequence.

Below programs illustrate the Ds\Sequence::sorted() function in PHP:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
   
// Create new sequence
$seq new \Ds\Vector([2, 4, 1, 9, 6, 5, 12, 9]);
  
// Use sorted() function to sort
// the sequence element
print_r($seq->sorted());
  
?>

chevron_right


Output:

Ds\Vector Object
(
    [0] => 1
    [1] => 2
    [2] => 4
    [3] => 5
    [4] => 6
    [5] => 9
    [6] => 9
    [7] => 12
)

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
   
// Create new sequence
$seq new \Ds\Vector(["Geeks", "GFG", "Abc", "for"]);
  
// Use sorted() function to sort
// the sequence element
print_r($seq->sorted());
  
?>

chevron_right


Output:

Ds\Vector Object
(
    [0] => Abc
    [1] => GFG
    [2] => Geeks
    [3] => for
)

Reference: http://php.net/manual/en/ds-sequence.sorted.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.