PHP Ds\Set sort() Function Last Updated : 11 Jul, 2025 Comments Improve Suggest changes Like Article Like Report The Ds\Set::sort() function of DS\Set class in PHP is used to in-place sort the elements of a specified Set instance according to the values. By default, the Set is sorted according to the increasing order of the values. Syntax: void public Ds\Set::sort ([ 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 second element. -1: if the first element is expected to be greater than second element. 0: if the first element is expected to be equal to the second element. Return value: The function does not returns any value. It just sorts the specified Set instance according to the comparator function passed. Below programs illustrate the Ds\Set::sort() function: Program 1: php <?php // PHP program to illustrate Ds\Set::sort() function $set = new \Ds\Set([20, 10, 30]); // sort the Set $set->sort(); // Print the sorted Set print_r($set); ?> Output: Ds\Set Object ( [0] => 10 [1] => 20 [2] => 30 ) Program 2: php <?php // PHP program to illustrate sort() function $set = new \Ds\Set([20, 10, 30]); // Declaring comparator function $comp = function($first, $second){ if($first>$second) return -1; else if($first<$second) return 1; else return 0; }; // sort the Set using comparator $set->sort($comp); // Print the sorted Set print_r($set); ?> Output: Ds\Set Object ( [0] => 30 [1] => 20 [2] => 10 ) Reference: https://www.php.net/manual/en/ds-set.sort.php Create Quiz Comment G gopaldave Follow 0 Improve G gopaldave 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