Skip to content
Related Articles

Related Articles

PHP | Ds\Sequence sort() Function
  • Last Updated : 22 Aug, 2019

The Ds\Sequence::sort() function is an inbuilt function in PHP which is used to sort the sequence element in the same place.

Syntax:

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

Parameters: This function accepts a single parameter $comparator which is used to hold the comparison function. The comparison function returns an integer value less then, greater then or equal to zero.

Return value: This function does not returns any values.

Below programs illustrate the Ds\Sequence::sort() function in PHP:



Program 1:




<?php
   
// Create new sequence
$seq new \Ds\Vector([2, 4, 1, 9, 6, 5, 12, 9]);
  
// Use sort() function to sort
// the sequence element
$seq->sort();
  
print_r($seq);
  
?>

Output:

Ds\Vector Object
(
    [0] => 1
    [1] => 2
    [2] => 4
    [3] => 5
    [4] => 6
    [5] => 9
    [6] => 9
    [7] => 12
)

Program 2:




<?php
   
// Create new sequence
$seq new \Ds\Vector([2, 4, 1, 9, 6, 5, 12, 9]);
  
// Use sort() function to sort
// the sequence element
$seq->sort(function($x, $y) {
    return $y <=> $x;
});
  
print_r($seq);
  
?>

Output:

Ds\Vector Object
(
    [0] => 12
    [1] => 9
    [2] => 9
    [3] => 6
    [4] => 5
    [5] => 4
    [6] => 2
    [7] => 1
)

Reference: http://php.net/manual/en/ds-sequence.sort.php

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :