PHP Ds / Set sort () Function

PHP

Syntax :
void public DsSet::sort ([callable $comparator])
Parameters :This function takes a comparator function according to which the values ​​are compared when sorting the set. The comparator should return the following values ​​based on a comparison of the two values ​​passed to it as a parameter:
  • 1:if the first element is expected to be less than the second element.
  • -1:if the first element is expected to be larger than the second.
  • 0:if the first is expected the element will be equal to the second element.
Return value:The function does not return any value. It simply sorts the specified Set instance according to the passed compare function.The following programs illustrate the Ds / Set::sort()function:Program 1:
// PHP program to illustrate the function Ds / Set::sort() $set = new DsSet ([20, 10, 30]) ;  
// sort the set $set -> sort();  
// Print a sorted set print_r ( $set );  
?>
Exit:
DsSet Object ( [0] = > 10 [1] = > 20 [2] = > 30)
Program 2:
// PHP program to illustrate the sort() function $set = new DsSet ([20, 10, 30]);  
// Comparator function declaration $comp = function ( $first , $second ) { if ( $first > $second ) return -1; else if ( $first < $second ) return 1; else return 0; };  
// sort the set using a comparator $set -> sort ( $comp );  
// Print a sorted set print_r ( $set );  
?>
Exit:
DsSet Object ( [0] = > 30 [1] = > 20 [2] = > 10)
Link : http://php.net/manual/en/ds-set.sort.php



Get Solution for free from DataCamp guru