PHP - Ds Vector sort() Function



The PHP Ds\Vector::sort() function is used to sort the vector in-place. The term sort in-place means that it modifies the original vector without creating or storing any new memory.

This function accepts an optional parameter using which you can specify the criteria, and based on the return value the element in the vector will be sorted.

Below are the lists of some important points of the comparator function −

  • It returns -1, if the first element is greater than the second element.
  • It returns 1, if the first element is less than the second element.
  • It returns 0, if the first element is equal to the second element.

Syntax

Following is the syntax of the PHP Ds\Vector::sort() function −

public Ds\Vector::sort(callable $comparator = ?): void

Parameters

This function accepts a single parameter named 'comparator' function, which is described below −

  • comparator − An optional comparator function compares the elements and returns an integer value.

Following is the syntax of the comparator function −

callback(mixed $a, mixed $b): int

Return value

This function does not return any value.

Example 1

If the comparator function is omitted the element of the vector will be automatically sorted in ascending order by default −

<?php 
   $vector = new \Ds\Vector([60, 50, 40, 30, 20, 10]); 
   echo("The original vector: \n"); 
   print_r($vector);
   #using sort() function
   $vector->sort();
   echo("\nThe vector after sorted: \n"); 
   print_r($vector);
?>

Output

The above program produces the following output −

The original vector:
Ds\Vector Object
(
    [0] => 60
    [1] => 50
    [2] => 40
    [3] => 30
    [4] => 20
    [5] => 10
)

The vector after sorted:
Ds\Vector Object
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 40
    [4] => 50
    [5] => 60
)

Example 2

Following is another example of the PHP Ds\Vector::sort() function. We use this function to sort the order of this vector ([2, 5, 8, 1, 6, 3]) element by using the comparator function −

<?php 
   $vector = new \Ds\Vector([2, 5, 8, 1, 6, 3]); 
   echo("The original vector: \n"); 
   print_r($vector);
   #using sort() function
   $vector->sort(function($element1, $element2) { 
      return $element2 <=> $element1; 
   });
   echo("The decreasing sorted elements: \n");
   print_r($vector); 
?>

Output

After executing the above program, the following output will be displayed −

The original vector:
Ds\Vector Object
(
    [0] => 2
    [1] => 5
    [2] => 8
    [3] => 1
    [4] => 6
    [5] => 3
)
The decreasing sorted elements:
Ds\Vector Object
(
    [0] => 8
    [1] => 6
    [2] => 5
    [3] => 3
    [4] => 2
    [5] => 1
)

Example 3

In the example below, we use the sort() function to sort the order of the vector elements −

<?php 
   $vector = new \Ds\Vector(['d', 'a', 'c', 'e', 'b']); 
   echo("The original vector: \n"); 
   print_r($vector);
   #using sort() function
   $vector->sort();
   echo("\nThe decreasing sorted elements: \n");
   print_r($vector); 
?>

Output

Once the above program is executed, it generates the following output −

The original vector:
Ds\Vector Object
(
    [0] => d
    [1] => a
    [2] => c
    [3] => e
    [4] => b
)

The decreasing sorted elements:
Ds\Vector Object
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
    [4] => e
)
php_function_reference.htm
Advertisements