PHP rsort Function | Sort Array in Descending Order

PHP rsort is an inbuilt function in PHP. It sorts an array in descending order i.e, from bigger to smaller element. Also, it makes the changes in the original array itself. The function provides 6 sorting types to sort an input array. In this article, we will discuss the PHP rsort Function.

In order to sort an array in ascending order, you can use PHP sort Function.

Also, like most PHP sorting Functions, it uses an implementation of Quicksort. The pivot is chosen in the middle of the partition resulting in an optimal time for already sorted arrays.

Syntax

boolean rsort($array, $sortingType = SORT_REGULAR)

Parameters

The PHP rsort Function expects two parameters. However, only the first parameter is mandatory.

  • $array: The first parameter to the function is an array which you need to sort in descending order. Also, it is a mandatory parameter.
  • $sortingType: The function provides 6 sorting types which are optional to pass. This parameter can take the following values:
    • SORT_REGULAR: On passing SORT_REGULAR or 0, the function compares the elements normally without changing their type. Also, this is the default value to the parameter.
    • SORT_NUMERIC: On passing SORT_NUMERIC or 1, the function compares the elements numerically.
    • SORT_STRING: The function compares the elements string-wise on passing SORT_STRING or 2.
    • SORT_LOCALE_STRING: The function compares the elements as a string based on current locale on passing SORT_LOCALE_STRING or 3. It uses the locale which can be changed by the setlocale function.
    • SORT_NATURAL: On passing 4 or SORT_NATURAL, the function compares the elements as strings using natural ordering.
    • SORT_FLAG_CASE: On passing 5 or SORT_FLAG_CASE, the function compares the elements as case-insensitive strings. Also, this parameter can be used | (bitwise) with SORT_NATURAL or SORT_STRING.

Return Value

The PHP rsort Function returns true on success and false on failure. However, it modifies the original input array.

Note: The function assigns new keys to the elements in the array. It will remove any existing keys rather than re-ordering.

PHP rsort Function Diagram

Examples

Let’s discuss a few examples of using the rsort Function in PHP.

Example 1: Sort Integer Array in Descending Order

<?php

	$array = array(5, 1, 2, 7, 3);
	rsort($array);
	print_r($array);
	/*
	Array
	(
	    [0] => 7
	    [1] => 5
	    [2] => 3
	    [3] => 2
	    [4] => 1
	)
	*/

?>

You can sort an integer array in descending order by passing it to the rsort function. Also, it modifies the original array itself.

Example 2: Sort Strings in Descending Order

Similarly, you can sort an array containing strings.

<?php

	$array = array('banana', 'cat', 'apple');
	rsort($array);
	print_r($array);
	/*
	Array
	(
	    [0] => cat
	    [1] => banana
	    [2] => apple
	)
	*/

?>

Example 3: Case Sensitivity in Strings

You can observe the difference in the output when SORT_FLAG_CASE is passed along with SORT_STRING in the below example:

<?php

	$array = array("concatly", "Ravi", "striver", "codeblocks", "RAvi");
	
	//Function sorts case sensitively
	rsort($array, SORT_STRING);
	print_r($array);
	/*
	Array
	(
	    [0] => striver
	    [1] => concatly
	    [2] => codeblocks
	    [3] => Ravi
	    [4] => RAvi
	)
	*/

	//Function sorts case insensitively
	rsort($array, SORT_STRING | SORT_FLAG_CASE);
	print_r($array);
	/*
	Array
	(
	    [0] => striver
	    [1] => Ravi
	    [2] => RAvi
	    [3] => concatly
	    [4] => codeblocks
	)
	*/

?>

Conclusion

In conclusion, we discussed the PHP rsort Function in the above article. You can read more about it on PHP Official Documentation. Additionally, you can about more PHP Array Functions on Concatly.

Spread the Knowledge

Leave a Reply

Your email address will not be published. Required fields are marked *