Current Location: Home> Function Categories> rsort

rsort

Reverse sorting of arrays
Name:rsort
Category:Array
Programming Language:php
One-line Description:Reversely sort the array.

Definition and usage

rsort() function sorts the numeric array in descending order.

Tip: Please use the sort() function to sort the numeric array in ascending order.

Example

Example 1

Sort elements in array $cars in descending order:

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

Try it yourself

Example 2

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

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

Try it yourself

Example 3

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 ) ;
?>

Try it yourself

grammar

 rsort ( array , sortingtype ) ;
parameter describe
array Required. Specifies the array to be sorted.
sortingtype

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

  • 0 = SORT_REGULAR - Default. Arrange each item in a regular order (Standard ASCII, without changing the type)
  • 1 = SORT_NUMERIC - Treat each item as a number.
  • 2 = SORT_STRING - Handle each item as a string.
  • 3 = SORT_LOCALE_STRING - Handle each item as a string, based on the current locale setting (can be changed via setlocale()).
  • 4 = SORT_NATURAL - Handle each item as a string, using a natural sort like natsort().
  • 5 = SORT_FLAG_CASE - Strings can be sorted in combination with (bit-by-bit or) SORT_STRING or SORT_NATURAL, case-insensitive.

illustrate

The rsort() function reverses the elements of the array by key values. The function is basically the same as arsort() .

Note: This function assigns a new key name to the cells in the array . This will remove the original key name instead of reordering.

Return TRUE if successful, otherwise return FALSE.

The optional second parameter contains additional sorting flags.

Similar Functions
Popular Articles