PHP - Ds Map::sorted() Function



The PHP Ds\Map::sorted() function is used to retrieve a copy of a map sorted by value. The values of the returned map will be arrange in ascending order by default.

This function accepts an optional comparison function, which returns an integer less than, equal, or greater than zero if the argument is considered to be respectively less than, equal to, or greater than the second.

Syntax

Following is the syntax of the PHP Ds\Map::sorted() function −

public Ds\Map Ds\Map::sorted([ callable $comparator ] )

Parameters

This function accepts a single optional parameter as a comparator function, which is described below −

  • comparator − A comparison function returns an integer value.

Return value

This function returns a copy of the current map sorted by value.

Example 1

The following is the basic example of the PHP Ds\Map::sorted() function −

<?php 
   $map = new \Ds\Map([3, 5, 2, 1, 4]);
   echo "The map elements are: \n";
   print_r($map);
   echo "The sorted copy of a map is: \n";
   print_r($map->sorted());  
?>

Output

The above program produces the following output −

The map elements are:
Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => 0
            [value] => 3
        )

    [1] => Ds\Pair Object
        (
            [key] => 1
            [value] => 5
        )

    [2] => Ds\Pair Object
        (
            [key] => 2
            [value] => 2
        )

    [3] => Ds\Pair Object
        (
            [key] => 3
            [value] => 1
        )

    [4] => Ds\Pair Object
        (
            [key] => 4
            [value] => 4
        )

)
The sorted copy of a map is:
Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => 3
            [value] => 1
        )

    [1] => Ds\Pair Object
        (
            [key] => 2
            [value] => 2
        )

    [2] => Ds\Pair Object
        (
            [key] => 0
            [value] => 3
        )

    [3] => Ds\Pair Object
        (
            [key] => 4
            [value] => 4
        )

    [4] => Ds\Pair Object
        (
            [key] => 1
            [value] => 5
        )

)

Example 2

Using the comparator function.

Following is another example of the PHP Ds\Map::sorted() function. We use this function to retrieve a copy of this ([50, 20, 40, 10, 30]) map sorted by value using the comparator function −

<?php 
   $map = new \Ds\Map([50, 20, 40, 10, 30]);
   echo "The map elements are: \n";
   print_r($map);
   echo "The sorted copy of a map is: \n";
   #using comparator function
   $copy_map = $map->sorted(function($a, $b){
	   return $a<=>$b;
   });
   print_r($copy_map);
?>

Output

After executing the above program, it will display the following output −

The map elements are:
Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => 0
            [value] => 50
        )

    [1] => Ds\Pair Object
        (
            [key] => 1
            [value] => 20
        )

    [2] => Ds\Pair Object
        (
            [key] => 2
            [value] => 40
        )

    [3] => Ds\Pair Object
        (
            [key] => 3
            [value] => 10
        )

    [4] => Ds\Pair Object
        (
            [key] => 4
            [value] => 30
        )

)
The sorted copy of a map is:
Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => 3
            [value] => 10
        )

    [1] => Ds\Pair Object
        (
            [key] => 1
            [value] => 20
        )

    [2] => Ds\Pair Object
        (
            [key] => 4
            [value] => 30
        )

    [3] => Ds\Pair Object
        (
            [key] => 2
            [value] => 40
        )

    [4] => Ds\Pair Object
        (
            [key] => 0
            [value] => 50
        )

)
php_function_reference.htm
Advertisements