rsort

Sorts an array by value in descending order and reindexes it with fresh numeric keys.

Signature
rsort(array &$array, int $flags = SORT_REGULAR): true
  • Added in PHP 4.0

Parameters

ParameterTypeDescription
$arrayarrayThe array to sort. Passed by reference; existing keys are discarded and replaced with 0-based integers.
$flagsoptionalint= SORT_REGULARComparison mode: SORT_REGULAR, SORT_NUMERIC, SORT_STRING, SORT_LOCALE_STRING or SORT_NATURAL, optionally combined with SORT_FLAG_CASE.

Return value

trueAlways returns true. The array is sorted in place through the reference.

Errors

Nothing. A non-array first argument raises a TypeError from the parameter type.

Changelog

  • PHP 8.2The return type is true now; previously it was bool.
  • PHP 8.0Elements that compare as equal keep their original order (stable sort), and numeric strings under SORT_REGULAR follow the PHP 8 comparison rules.

rsort is sort running downhill: values are ordered largest to smallest and the array is renumbered from 0. Any string or non-sequential keys you had are thrown away.

When to reach for it

Leaderboards, top-N lists, newest-first timestamps, largest file sizes — anything where you want a plain list already in descending order and the original keys carry no meaning. Because it reindexes, the result is a clean JSON array, which makes it convenient right before array_slice($scores, 0, 10) for a top-ten.

What trips people up

Losing the keys is the number one surprise. If your array maps names to scores, rsort leaves you with the scores and no idea who they belong to — use arsort, which does the same descending sort but keeps the associations.

The default SORT_REGULAR compares mixed types with PHP's normal rules, and in PHP 8 those rules changed: a numeric string is compared numerically against a number, but a non-numeric string is compared as a string. Sorting a mix of '10', 5 and 'apple' gives results that are consistent but rarely what you meant. Pass SORT_NUMERIC or SORT_STRING to state your intent, and use usort when the comparison needs real logic.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Build a top-10 leaderboard by rsorting the scores and slicing the first ten.
  • Order file sizes or byte counts largest first for a disk-usage report.
  • Show the most recent Unix timestamps first in an activity feed.

Frequently asked questions

What is the difference between rsort and arsort?

Both sort by value in descending order, but rsort discards the keys and renumbers from 0 while arsort preserves the key/value associations. Use arsort for associative arrays.

Does rsort return the sorted array?

No, it returns true and modifies the array in place. Assigning $x = rsort($arr) gives you the boolean, not the sorted data.

Is rsort the same as sort followed by array_reverse?

For distinct values, yes. They differ for equal elements: rsort is stable, keeping equal items in their original order, while sort plus array_reverse flips that order.

More examples

rsort versus arsort

rsort prints [95,88,72] — correctly ordered, but the player names are gone. arsort prints {"bo":95,"ana":88,"cy":72}, keeping each score attached to its player, which is almost always what a leaderboard needs.

Example · php
<?php
$players = ['ana' => 88, 'bo' => 95, 'cy' => 72];

$a = $players; rsort($a);
$b = $players; arsort($b);

echo 'rsort:  ', json_encode($a), "\n";
echo 'arsort: ', json_encode($b), "\n";

Flags change the ordering

As strings the order is 9, 100, 10 because '9' is byte-greater than '1'. As numbers it is 100, 10, 9. Numeric-looking data read from a CSV or a form is always strings, so the flag matters.

Example · php
<?php
$mixed = ['10', '9', '100'];

$s = $mixed; rsort($s, SORT_STRING);
$n = $mixed; rsort($n, SORT_NUMERIC);

echo 'SORT_STRING:  ', implode(' ', $s), "\n";
echo 'SORT_NUMERIC: ', implode(' ', $n), "\n";

Discussion

  • Be the first to comment on this lesson.