PHP | Ds\Deque sort() Function Last Updated : 11 Jul, 2025 Comments Improve Suggest changes Like Article Like Report The Ds\Deque::sort() function is an inbuilt function in PHP which is used to sort the Deque in place by arranging the elements in increasing order. Syntax: public Ds\Deque::sort( $comparator ) : void Parameters::This function accepts single parameter $comparator which holds the function to decides how to sort the elements. It helps in customizing the sort function. Return Value: This function does not return any value. Below programs illustrate the Ds\Deque::sort() function in PHP: Program 1: PHP <?php // Declare a deque $deck = new \Ds\Deque([5, 6, 3, 2, 7, 1]); echo("Elements of Deque\n"); // Display the Deque elements print_r($deck); // Use sort() function to sort // the elements $deck->sort(); echo("Sorted Deque\n"); // Display the Deque elements print_r($deck);; ?> Output: Elements of Deque Ds\Deque Object ( [0] => 5 [1] => 6 [2] => 3 [3] => 2 [4] => 7 [5] => 1 ) Sorted Deque Ds\Deque Object ( [0] => 1 [1] => 2 [2] => 3 [3] => 5 [4] => 6 [5] => 7 ) Program 2: PHP <?php // Declare a deque $deck = new \Ds\Deque([5, 6, 3, 2, 7, 1]); echo("Elements of Deque\n"); // Display the Deque elements print_r($deck); // comparator to sort in reverse order $deck->sort(function($first, $second) { return $first <= $second; }); echo("Sorted Deque\n"); // Display the Deque elements print_r($deck);; ?> Output: Elements of Deque Ds\Deque Object ( [0] => 5 [1] => 6 [2] => 3 [3] => 2 [4] => 7 [5] => 1 ) Sorted Deque Ds\Deque Object ( [0] => 7 [1] => 6 [2] => 5 [3] => 3 [4] => 2 [5] => 1 ) Reference: https://www.php.net/manual/en/ds-deque.sort.php Create Quiz Comment B barykrg Follow 0 Improve B barykrg Follow 0 Improve Article Tags : Web Technologies PHP PHP-function PHP-ds_deque Explore BasicsPHP Syntax4 min readPHP Variables5 min readPHP | Functions6 min readPHP Loops4 min readArrayPHP Arrays5 min readPHP Associative Arrays4 min readMultidimensional arrays in PHP5 min readSorting Arrays in PHP4 min readOOPs & InterfacesPHP Classes2 min readPHP | Constructors and Destructors5 min readPHP Access Modifiers4 min readMultiple Inheritance in PHP4 min readMySQL DatabasePHP | MySQL Database Introduction4 min readPHP Database connection2 min readPHP | MySQL ( Creating Database )3 min readPHP | MySQL ( Creating Table )3 min readPHP AdvancePHP Superglobals6 min readPHP | Regular Expressions12 min readPHP Form Handling4 min readPHP File Handling4 min readPHP | Uploading File3 min readPHP Cookies9 min readPHP | Sessions7 min read Like