PHP | rsort() Function - GeeksforGeeks

05 Nov, 2020
The rsort() is an inbuilt function in PHP and is used to sort the array in descending order i.e, greatest to smallest. It sorts the actual array and hence changes are reflected in the array itself. The function provides us with 6 sorting types, according to which the array can be sorted. 
Syntax: 
rsort($array, sorting_type)


Parameters: 
$array: This parameter specifies the array which we want ot sort.sorting_type: This is an optional parameter. This parameter specifies the mode in which the sort operation will be performed on the input array. There are 6 sorting types which are described below: SORT_REGULAR – When we pass 0 or SORT_REGULAR in the sorting_type parameter, the items in the array are compared normallySORT_NUMERIC – When we pass 1 or SORT_NUMERIC in the sorting_type parameter, the items in the array are compared numericallySORT_STRING – When we pass 2 or SORT_STRING in the sorting_type parameter, the items in the array are compared string-wiseSORT_LOCALE_STRING – When we pass 3 or SORT_LOCALE_STRING in the sorting_type parameter, the items in the array are compared as string based on current localeSORT_NATURAL – When we pass 4 or SORT_NATURAL in the sorting_type parameter, the items in the array are compared as string using natural orderingSORT_FLAG_CASE – When we pass 5 or SORT_FLAG_CASE in the sorting_type parameter, the items in the array are compared as strings. The items are treated as case-insensitive and then compared. It can be used using | (bitwise operator) with SORT_NATURAL or SORT_STRING.
$array: This parameter specifies the array which we want ot sort.
sorting_type: This is an optional parameter. This parameter specifies the mode in which the sort operation will be performed on the input array. There are 6 sorting types which are described below: SORT_REGULAR – When we pass 0 or SORT_REGULAR in the sorting_type parameter, the items in the array are compared normallySORT_NUMERIC – When we pass 1 or SORT_NUMERIC in the sorting_type parameter, the items in the array are compared numericallySORT_STRING – When we pass 2 or SORT_STRING in the sorting_type parameter, the items in the array are compared string-wiseSORT_LOCALE_STRING – When we pass 3 or SORT_LOCALE_STRING in the sorting_type parameter, the items in the array are compared as string based on current localeSORT_NATURAL – When we pass 4 or SORT_NATURAL in the sorting_type parameter, the items in the array are compared as string using natural orderingSORT_FLAG_CASE – When we pass 5 or SORT_FLAG_CASE in the sorting_type parameter, the items in the array are compared as strings. The items are treated as case-insensitive and then compared. It can be used using | (bitwise operator) with SORT_NATURAL or SORT_STRING.
SORT_REGULAR – When we pass 0 or SORT_REGULAR in the sorting_type parameter, the items in the array are compared normally
SORT_NUMERIC – When we pass 1 or SORT_NUMERIC in the sorting_type parameter, the items in the array are compared numerically
SORT_STRING – When we pass 2 or SORT_STRING in the sorting_type parameter, the items in the array are compared string-wise
SORT_LOCALE_STRING – When we pass 3 or SORT_LOCALE_STRING in the sorting_type parameter, the items in the array are compared as string based on current locale
SORT_NATURAL – When we pass 4 or SORT_NATURAL in the sorting_type parameter, the items in the array are compared as string using natural ordering
SORT_FLAG_CASE – When we pass 5 or SORT_FLAG_CASE in the sorting_type parameter, the items in the array are compared as strings. The items are treated as case-insensitive and then compared. It can be used using | (bitwise operator) with SORT_NATURAL or SORT_STRING.
Return Value: It returns a boolean value, TRUE on success and False in failure. It sorts the original array in descending which is passed as a parameter to it. 
Examples: 
Input : $array = [3, 4, 1, 2] 
Output : 
Array
(
    [0] => 4
    [1] => 3
    [2] => 2
    [3] => 1
)


Input : $array = ["geeks2", "raj1", "striver3", "coding4"]
Output :
Array
(
    [0] => striver3 
    [1] => raj1 
    [2] => geeks2 
    [3] => coding4
)


Below programs illustrate the rsort() function in PHP:
Program 1: Program to demonstrate the use of rsort() function in descending order.  
PHP
<?php// PHP program to demonstrate the use of rsort() function $array = array(3, 4, 2, 1); // sorting function usedrsort($array); //prints the sorted arrayprint_r($array);?>
Output: 
Array
(
    [0] => 4
    [1] => 3
    [2] => 2
    [3] => 1
)


Program 2: Program to demonstrate the use of rsort() function to sort the string case-sensitively in descending order. 
PHP
<?php// PHP program to demonstrate the use of rsort() function// sorts the string case-sensitively$array = array("geeks", "Raj", "striver", "coding", "RAj"); // sorting function used, sorts the string case-sensitivelyrsort($array, SORT_STRING); // prints the sorted arrayprint_r($array);?>
Output: 
Array
(
    [0] => striver
    [1] => Raj
    [2] => RAj
    [3] => geeks
    [4] => coding
)


Program 3 : Program to demonstrate the use of rsort() function to sort the string case-insensitively in descending order. 
PHP
<?php// PHP program to demonstrate the use of rsort() function// sorts the string case-insensitively$array = array("geeks", "Raj", "striver", "coding", "RAj"); // sorting function used, sorts the// string case-insensitivelyrsort($array, SORT_STRING | SORT_FLAG_CASE); // prints the sorted arrayprint_r($array);?>
Output: 
Array
(
    [0] => striver
    [1] => Raj
    [2] => RAj
    [3] => geeks
    [4] => coding
)


Reference: http://php.net/manual/en/function.rsort.php 
Akanksha_Rai
arorakashish0911
PHP-array
PHP
Web Technologies
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to fetch data from localserver database and display on HTML table using PHP ?
How to create admin login page using PHP?
PHP str_replace() Function
How to pass form variables from one page to other page in PHP ?
Different ways for passing data to view in Laravel
Remove elements from a JavaScript Array
Installation of Node.js on Linux
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
Top 10 Projects For Beginners To Practice HTML and CSS Skills