PHP | Ds\Map sorted() Function Last Updated : 21 Aug, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report The Ds\Map::sorted() function is an inbuilt function in PHP which is used to get a copy of the specified Map instance sorted according to the values. By default, the copy of Map is sorted according to the increasing order of the values. Syntax: Ds\Map public Ds\Map::sorted( $comparator ) Parameter: This function accepts single parameter $comparator which contains function according to which the values will be compared while sorting the copy of the Map. The comparator should return the following values based on the comparison on 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 returns the copy of the Map sorted according to the values. Note: This function does not modifies or affect the actual Map instance. Below programs illustrate the Ds\Map::sorted() function in PHP: Program 1: php <?php // PHP program to illustrate sorted() function // Declare a Map $map = new \Ds\Map([1 => 20, 2 => 10, 3 => 30]); // Print the sorted copy Map print_r($map->sorted()); ?> Output: Ds\Map Object ( [0] => Ds\Pair Object ( [key] => 2 [value] => 10 ) [1] => Ds\Pair Object ( [key] => 1 [value] => 20 ) [2] => Ds\Pair Object ( [key] => 3 [value] => 30 ) ) Program 2: php <?php // PHP program to illustrate sorted() function // Declare a Map $map = new \Ds\Map([1 => 20, 2 => 10, 3 => 30]); // Declaring comparator function $comp = function($first, $second){ if($first>$second) return -1; else if($first<$second) return 1; else return 0; }; // Print the sorted copy Map print_r($map->sorted()); ?> Output: Ds\Map Object ( [0] => Ds\Pair Object ( [key] => 3 [value] => 30 ) [1] => Ds\Pair Object ( [key] => 1 [value] => 20 ) [2] => Ds\Pair Object ( [key] => 2 [value] => 10 ) ) Reference: http://php.net/manual/en/ds-map.sorted.php Comment More infoAdvertise with us Next Article PHP | Ds\Map sorted() Function G gopaldave Follow Improve Article Tags : Web Technologies PHP PHP-function PHP-ds_map Similar Reads PHP | DsSet sorted() Function 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 2 min read PHP | DsDeque sorted() Function The Ds\Deque::sorted() function is an inbuilt function in PHP which is used to return a copy of Deque which contains the element in the original Deque in increasing order. Syntax: public Ds\Deque::sorted( $comparator ) : Ds\Deque Parameters: This function accepts single parameter $comparator which h 2 min read PHP | DsMap sort() Function The Ds\Map::sort() function of DS\Map class in PHP is used to in-place sort the elements of a specified Map instance according to the values. By default, the Map is sorted according to the increasing order of the values. Syntax: Ds\Pair public Ds\Map::sort ( int $position ) Parameter: This function 2 min read PHP DsSet sort() Function 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 f 2 min read PHP | DsVector sorted() Function The Ds\Vector::sorted() function is an inbuilt function in PHP which is used to sort the elements of the vector by creating a copy of the original vector. This will arrange the vector elements in increasing order using default comparator. Syntax: Ds\Vector public Ds\Vector::sorted( $comparator ) Par 2 min read Like