• PHP Video Tutorials

PHP - Ds Set sorted() Function



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

Syntax

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

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

Example 1

<?php   
   $set = new \Ds\Set([20, 10, 30, 50, 40]);  
   print_r($set->sorted());  
?> 

Example 2

<?php  
   $set = new \Ds\Set([2, 4, 8, 3, 6, 1, 5]);  
   
   $sorted = $set->sorted(function($x, $y) { 
      return $y <=> $x; 
   }); 
   print_r($sorted); 
?>
php_function_reference.htm
Advertisements