PHP Function rsort()
In this article, we will be discussing the PHP function array_rsort(), its syntax, parameters, and how it works. We will also provide some practical examples of
In this article, we will be discussing the PHP function rsort(), its syntax, parameters, and how it works. We will also provide some practical examples of using this function in real-world scenarios.
Introduction
Sorting is an essential operation when working with arrays in PHP. The rsort() function sorts an array in place in reverse (descending) order by value — the largest value ends up at index 0. It is the descending counterpart of sort(), and it discards the original keys, re-indexing the array from 0.
Reach for rsort() when you have an indexed list (a leaderboard, a list of prices, recent timestamps) and you want the highest values first without caring about the original positions.
Syntax
The syntax for the rsort() function is as follows:
rsort($array, $flags);The function takes two parameters: $array and $flags. The $array parameter is mandatory and represents the array that needs to be sorted. The $flags parameter is optional and represents the sorting behavior. It can take the following values:
SORT_REGULAR: Compare items normally (don't change types)SORT_NUMERIC: Compare items numericallySORT_STRING: Compare items as stringsSORT_LOCALE_STRING: Compare items as strings based on the current system locale settings.
How it Works
The rsort() function works by comparing the values in the array and sorting them in reverse order. It compares each value with the next one in the array and swaps them if they are not in the correct order. This process continues until the entire array is sorted.
Note that rsort() modifies the original array in-place and returns a boolean (true on success, false on failure). It also reindexes numeric keys sequentially after sorting.
Practical Examples
Let's take a look at some practical examples of using the rsort() function.
Example 1 - Sorting Numeric Values
Output:
Array
(
[0] => 20
[1] => 15
[2] => 10
[3] => 8
[4] => 5
)Example 2 - Sorting String Values
Output:
Array
(
[0] => elderberry
[1] => date
[2] => cherry
[3] => banana
[4] => apple
)Example 3 - Sorting Mixed Values
Output:
Array
(
[0] => cherry
[1] => banana
[2] => apple
[3] => 10
[4] => 8
[5] => 5
)With SORT_REGULAR, the strings sort above the numbers because PHP compares values without forcing a type cast. Be aware that comparing mixed types can produce surprising results; when your data is genuinely mixed, prefer an explicit flag or a custom comparison with usort().
Common Gotchas
- Keys are lost.
rsort()always re-indexes the array starting at0, so any existing keys (string or numeric) are discarded. If you need to keep the key-to-value association, usearsort()instead. - It sorts by value, not by key. To sort by key in reverse order, use
krsort(). - It modifies the original array. Because
rsort()works in place and returns onlytrue/false, never write$sorted = rsort($array);— that would store the boolean, not the sorted array.
Related Functions
| Function | Sorts by | Order | Keeps keys? |
|---|---|---|---|
sort() | Value | Ascending | No |
rsort() | Value | Descending | No |
asort() | Value | Ascending | Yes |
arsort() | Value | Descending | Yes |
ksort() | Key | Ascending | Yes |
krsort() | Key | Descending | Yes |
For a broader overview of PHP's sorting tools, see Sorting Arrays.
Conclusion
In this article, we have discussed the rsort() function in PHP, covering its syntax, parameters, and behavior. We also provided practical examples of sorting numeric, string, and mixed arrays.
If you need to sort an array in reverse order according to its values, rsort() is a reliable built-in tool. By understanding its in-place modification behavior and available flags, you can efficiently handle array sorting in your PHP projects.