PHP rsort() function

Example

Sort the elements of the array $cars in alphabetical order in descending order:

<?php
$cars=array("Volvo","BMW","Toyota");
rsort($cars);
?>

Run Instances

Definition and Usage

The rsort() function sorts a numerical array in descending order.

Tip:Please use sort() The function sorts a numerical array in ascending order.

Syntax

rsort(array,sortingtype);
Parameter Description
array Required. Specify the array to be sorted.
sortingtype

Optional. Specify how to compare the elements/items of an array. Possible values:

  • 0 = SORT_REGULAR - Default. Arrange each item in regular order (Standard ASCII, without changing type)
  • 1 = SORT_NUMERIC - Treat each item as a number.
  • 2 = SORT_STRING - Treat each item as a string.
  • 3 = SORT_LOCALE_STRING - Treats each item as a string, based on the current locale (which can be changed via setlocale()).
  • 4 = SORT_NATURAL - Treats each item as a string, using a natural sorting similar to natsort().
  • 5 = SORT_FLAG_CASE - Can combine (bitwise OR) SORT_STRING or SORT_NATURAL to sort strings without case sensitivity.

Description

The rsort() function sorts the elements of an array in reverse order by key value. It is similar in function to arsort().

Note:This function is for array Assigns new key names to the units. This will delete the original key names rather than just reordering.

Returns TRUE if successful, otherwise FALSE.

The optional second parameter includes additional sorting flags.

Technical Details

Return Value: TRUE on success. FALSE on failure
PHP Version: 4+

More Examples

Example 1

Sort the elements in the array $numbers in descending order by number:

<?php
$numbers=array(4,6,2,22,11);
rsort($numbers);
?>

Run Instances

Example 2

Compare items as numbers and sort the elements in the array $cars in descending order:

<?php
$cars=array("Volvo","BMW","Toyota");
rsort($cars,SORT_NUMERIC);
?>

Run Instances