![]() |
VOOZH | about |
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:
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.
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.
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.
Output:
Array ( [0] => striver [1] => Raj [2] => RAj [3] => geeks [4] => coding )