w3resource

PHP Array Exercises : Sort an array in reverse order


28. Reverse Sort Array

Write a PHP script to sort an array in reverse order (highest to lowest).

Sample Solution:

PHP Code:

<?php
// Define an array of colors
$colors = array("Red", "Orange", "Black", "White");

// Sort the array in reverse order (descending)
rsort($colors);

// Display the sorted array
print_r($colors);
?>

Output:

Array                                                       
(                                                           
    [0] => White                                            
    [1] => Red                                              
    [2] => Orange                                           
    [3] => Black                                            
)

Flowchart:

Flowchart: PHP - Sort an array in reverse order

For more Practice: Solve these Related Problems:

  • Write a PHP script to sort an array of positive integers in reverse order without using rsort().
  • Write a PHP function to sort an array in descending order using a custom comparison function in usort().
  • Write a PHP program to implement reverse sorting by first sorting in ascending order, then reversing the array.
  • Write a PHP script to compare the output of a custom reverse sort algorithm with PHP's built-in functions.

Go to:


PREV : Generate Random Password Using Shuffle.
NEXT : Array Range from String.

PHP Code Editor:



Contribute your code and comments through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.