• PHP Video Tutorials

PHP - Ds Vector sorted() Function



Ds\Vector::sorted() function can return a sorted copy.

Syntax

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

Ds\Vector::sorted() function can return a sorted copy by using an optional comparator function.

Ds\Vector::sorted() function can return a sorted copy of the vector.

Example 1

<?php 
   $vector = new \Ds\Vector([6, 5, 4, 3, 2, 1]); 
   echo("The original vector: \n"); 
   var_dump($vector); 
  
   $result = $vector->sorted(); 
   echo("\n The sorted elements: \n"); 
   var_dump($result); 
?>

Example 2

<?php 
   $vector = new \Ds\Vector([2, 5, 8, 1, 4, 7]); 
   echo("The original vector: \n"); 
   var_dump($vector); 
  
   $result = $array->sorted(function($element1, $element2) { 
      return $element1 <=> $element2; 
   }); 
   
   echo("\n The sorted elements: \n"); 
   var_dump($result); 
?>
php_function_reference.htm
Advertisements