• PHP Video Tutorials

PHP - Ds Vector sort() Function



Ds\Vector::sort() function can sort the vector in-place.

Syntax

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

Ds\Vector::sort() function can sort the vector in-place by using an optional comparator function.

Ds\Vector::sort() function doesn't return any value.

Example 1

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

Example 2

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