PHP | Ds\Set sorted() Function Last Updated : 31 Mar, 2022 Comments Improve Suggest changes Like Article Like Report The Ds\Set::sorted() function is an inbuilt function in PHP which is used to return a sorted copy of given set. Syntax: Ds\Set public Ds\Set::sorted ([ callable $comparator ]) Parameters: This function accepts a comparator function according to which the values will be compared while sorting the Set. The comparator should return the following values based on the comparison of two values passed to it as a parameter: 1: if the first element is expected to be less than the second element.-1: if the first element is expected to be greater than the second element.0: if the first element is expected to be equal to the second element. Return value: It returns the sorted copy of the given set. Below programs illustrate the Ds\Set::sorted() function in PHP: Program 1: php <?php // PHP program to illustrate sorted() function $set = new \Ds\Set([20, 10, 30]); // sort the Set print_r($set->sorted()); ?> Output:Ds\Set Object ( [0] => 10 [1] => 20 [2] => 30 ) Program 2: php <?php // Declare a new set $set = new \Ds\Set([2, 3, 6, 5, 7, 1, 4]); $sorted = $set->sorted(function($a, $b) { return $b <=> $a; }); print_r($sorted); ?> Output:Ds\Set Object ( [0] => 7 [1] => 6 [2] => 5 [3] => 4 [4] => 3 [5] => 2 [6] => 1 ) Reference: https://www.php.net/manual/en/ds-set.sorted.php Create Quiz Comment R R_Raj Follow 0 Improve R R_Raj Follow 0 Improve Article Tags : Web Technologies PHP PHP-function PHP-ds_set 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