In order to sort an array in ascending order, you can use PHP sort Function.
Also, like most PHP sorting Functions, it uses an implementation of Quicksort. The pivot is chosen in the middle of the partition resulting in an optimal time for already sorted arrays.
boolean rsort($array, $sortingType = SORT_REGULAR)
The PHP rsort Function expects two parameters. However, only the first parameter is mandatory.
- $array: The first parameter to the function is an array which you need to sort in descending order. Also, it is a mandatory parameter.
- $sortingType: The function provides 6 sorting types which are optional to pass. This parameter can take the following values:
- SORT_REGULAR: On passing SORT_REGULAR or 0, the function compares the elements normally without changing their type. Also, this is the default value to the parameter.
- SORT_NUMERIC: On passing SORT_NUMERIC or 1, the function compares the elements numerically.
- SORT_STRING: The function compares the elements string-wise on passing SORT_STRING or 2.
- SORT_LOCALE_STRING: The function compares the elements as a string based on current locale on passing SORT_LOCALE_STRING or 3. It uses the locale which can be changed by the
- SORT_NATURAL: On passing 4 or SORT_NATURAL, the function compares the elements as strings using natural ordering.
- SORT_FLAG_CASE: On passing 5 or SORT_FLAG_CASE, the function compares the elements as case-insensitive strings. Also, this parameter can be used | (bitwise) with SORT_NATURAL or SORT_STRING.
The PHP rsort Function returns true on success and false on failure. However, it modifies the original input array.
Note: The function assigns new keys to the elements in the array. It will remove any existing keys rather than
Let’s discuss a few examples of using the rsort Function in PHP.
Example 1: Sort Integer Array in Descending Order
<?php $array = array(5, 1, 2, 7, 3); rsort($array); print_r($array); /* Array (  => 7  => 5  => 3  => 2  => 1 ) */ ?>
You can sort an integer array in descending order by passing it to the
Example 2: Sort Strings in Descending Order
Similarly, you can sort an array containing strings.
<?php $array = array('banana', 'cat', 'apple'); rsort($array); print_r($array); /* Array (  => cat  => banana  => apple ) */ ?>
Example 3: Case Sensitivity in Strings
You can observe the difference in the output when SORT_FLAG_CASE is passed along with SORT_STRING in the below example:
<?php $array = array("concatly", "Ravi", "striver", "codeblocks", "RAvi"); //Function sorts case sensitively rsort($array, SORT_STRING); print_r($array); /* Array (  => striver  => concatly  => codeblocks  => Ravi  => RAvi ) */ //Function sorts case insensitively rsort($array, SORT_STRING | SORT_FLAG_CASE); print_r($array); /* Array (  => striver  => Ravi  => RAvi  => concatly  => codeblocks ) */ ?>
Vishesh is currently working as an Intermediate Software Engineer with Orion Health, New Zealand. He graduated with a Masters in Information Technology from the University of Auckland in 2021. With more than 4 years of work experience, his expertise includes Java, Python, Machine Learning, PHP, Databases, Design and Architecture.