Signature
rsort(array &$array, int $flags = SORT_REGULAR): truePurpose
rsort() reorders the values of an array from highest to lowest. It works directly on the variable you pass, so it changes the original array instead of returning a sorted copy. Use it when you only care about the values, for example a leaderboard of scores or a list of version numbers, and you do not need to keep the existing keys.
Signature
Parameters and defaults
$arrayis taken by reference. You must pass a variable (or another writable expression), not a literal array or the result of a function call.$flagscontrols how values are compared. The default isSORT_REGULAR, which uses PHP's normal comparison rules.
The flags documented for rsort() are:
SORT_REGULARcompares items normally.SORT_NUMERICcompares items numerically.SORT_STRINGcompares items as strings.SORT_LOCALE_STRINGcompares strings using the current locale, whichsetlocale()can change.SORT_NATURALcompares strings using natural ordering, asnatsort()does.SORT_FLAG_CASEcan be combined withSORT_STRINGorSORT_NATURALusing bitwise OR to ignore case.
Return value and failure cases
Since PHP 8.2 the declared return type is true; before that it was bool. The function always returns true, so checking the result tells you nothing. Real mistakes show up differently: passing something that cannot be taken by reference causes an error, and passing a non-array value is a type error.
Example 1: basic numeric sort
The highest value comes first, and duplicates are kept.
$scores = [72, 95, 88, 95, 60];
rsort($scores);
echo json_encode($scores), "\n";[95,95,88,72,60]
Example 2: keys are replaced
rsort() assigns new sequential keys. The original string keys are discarded, not merely reordered. The function also returns true.
$stock = ['b' => 5, 'a' => 12, 'c' => 8];
$result = rsort($stock);
var_dump($result);
echo json_encode($stock), "\n";bool(true) [12,8,5]
If the keys matter, use arsort() to sort by value descending while keeping the key association, or krsort() to sort by key.
Example 3: flags change the comparison
Numeric strings are compared as numbers under SORT_REGULAR, but as text under SORT_STRING. That produces different orders for the same data.
$values = ["10", "9", "2", "1"];
$regular = $values;
$string = $values;
rsort($regular, SORT_REGULAR);
rsort($string, SORT_STRING);
echo json_encode($regular), "\n";
echo json_encode($string), "\n";["10","9","2","1"] ["9","2","10","1"]
With SORT_STRING, "9" is greater than "2", which is greater than "10", because strings are compared character by character from the left.
Edge cases
- An empty array stays empty, and the function still returns
true. - Elements that compare as equal keep their original relative order. This is guaranteed from PHP 8.0.0; earlier versions left it undefined.
- The array's internal pointer is reset to the first element.
- Mixed value types (numbers, numeric strings, plain strings) follow the comparison rules for the chosen flag, so results can be surprising under
SORT_REGULAR. When your data is homogeneous, pick the matching flag explicitly. SORT_FLAG_CASEonly has an effect when combined withSORT_STRINGorSORT_NATURAL.
Common mistakes
- Assigning the result. Writing
$sorted = rsort($items);storestrue, not an array. Callrsort($items);and then use$items. - Expecting keys to survive. Associative keys are lost. Choose
arsort()when you need them. - Sorting a temporary value. Passing the return value of a function directly fails because the argument must be a variable. Assign it first.
- Ignoring the flag. Numeric strings such as
"10"and"9"can sort differently depending onSORT_STRINGversus numeric comparison. Set the flag when the type of comparison matters. - Using it for custom ordering.
rsort()has no comparison callback. For sorting by a field or a custom rule, use a user-defined comparison function with a function such asusort().
Related functions
sort()sorts values in ascending order.arsort()sorts values in descending order and keeps keys.krsort()sorts by key in descending order.
Recap
rsort() sorts an array's values from highest to lowest, in place, and replaces the keys with a fresh 0-based list. It takes an optional flag to control comparison, resets the internal pointer, keeps equal elements in their original order, and always returns true. Use the flag that fits your data, and do not rely on the return value.
Tested examples
$scores = [72, 95, 88, 95, 60];
rsort($scores);
echo json_encode($scores), "\n";
[95,95,88,72,60]
$stock = ['b' => 5, 'a' => 12, 'c' => 8];
$result = rsort($stock);
var_dump($result);
echo json_encode($stock), "\n";
bool(true) [12,8,5]
$values = ["10", "9", "2", "1"];
$regular = $values;
$string = $values;
rsort($regular, SORT_REGULAR);
rsort($string, SORT_STRING);
echo json_encode($regular), "\n";
echo json_encode($string), "\n";
["10","9","2","1"] ["9","2","10","1"]