Tuesday, October 7, 2025
HomeLanguagesPHP DsSequence sort() Function

PHP Ds\Sequence sort() Function

The Ds\Sequence::sort() function is an inbuilt function in PHP that 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 than, greater than, or equal to zero.

Return value: This function does not return any values. Below programs illustrate the Ds\Sequence::sort() function in PHP: 

Program 1: 

php




<?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




<?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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32340 POSTS0 COMMENTS
Milvus
86 POSTS0 COMMENTS
Nango Kala
6708 POSTS0 COMMENTS
Nicole Veronica
11872 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11936 POSTS0 COMMENTS
Shaida Kate Naidoo
6829 POSTS0 COMMENTS
Ted Musemwa
7090 POSTS0 COMMENTS
Thapelo Manthata
6780 POSTS0 COMMENTS
Umr Jansen
6784 POSTS0 COMMENTS