Friday, October 10, 2025
HomeLanguagesPHP | DsSequence sorted() Function

PHP | Ds\Sequence sorted() Function

The Ds\Sequence::sorted() function is an inbuilt function in PHP which is used to return the sorted copy of sequence element. Syntax:

Ds\Sequence abstract public Ds\Sequence::sorted( $comparator )

Parameters: This function accepts a single parameter $comparator which holds the comparison function. The comparison function returns an integer value which is less than or greater than or equal to zero. Return value: This function returns the sorted copy of sequence. Below programs illustrate the Ds\Sequence::sorted() function in PHP: Program 1: 

php




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


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(["Geeks", "GFG", "Abc", "for"]);
 
// Use sorted() function to sort
// the sequence element
print_r($seq->sorted());
 
?>


Output:

Ds\Vector Object
(
    [0] => Abc
    [1] => GFG
    [2] => Geeks
    [3] => for
)

Reference: http://php.net/manual/en/ds-sequence.sorted.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
32350 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6718 POSTS0 COMMENTS
Nicole Veronica
11880 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6838 POSTS0 COMMENTS
Ted Musemwa
7101 POSTS0 COMMENTS
Thapelo Manthata
6794 POSTS0 COMMENTS
Umr Jansen
6794 POSTS0 COMMENTS