Sort Indexed Arrays Descending
Part of the Logic & Flow section of Coddy's PHP journey — lesson 21 of 68.
When you need to sort data in reverse order—from highest to lowest or Z to A—PHP's rsort() function provides the perfect solution. This function works exactly like sort(), but arranges elements in descending order instead of ascending.
Here's how rsort() works with an array of scores:
<?php
$scores = [78, 95, 82, 91, 87];
rsort($scores);
print_r($scores);
// Outputs: Array ( [0] => 95 [1] => 91 [2] => 87 [3] => 82 [4] => 78 )
?>Just like sort(), the rsort() function modifies the original array directly rather than creating a new sorted copy. After calling rsort(), your array is permanently rearranged in descending order.
This function is particularly useful when you want to display rankings, leaderboards, or any data where the highest values should appear first. Whether you're working with test scores, sales figures, or alphabetical lists that need reverse ordering, rsort() handles the task efficiently.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
Challenge
EasyYou will receive one input: an array of sales figures in JSON format. Read the input, convert the JSON string to an array, use rsort() to arrange the sales figures in descending order, and print the sorted array using print_r().
Input format: One line containing a JSON array of numbers (example: [1250,3400,890,2100,1780])
Expected output: The sorted array in descending order, displayed using print_r()
Cheat sheet
The rsort() function sorts arrays in descending order (highest to lowest):
<?php
$scores = [78, 95, 82, 91, 87];
rsort($scores);
print_r($scores);
// Outputs: Array ( [0] => 95 [1] => 91 [2] => 87 [3] => 82 [4] => 78 )
?>Like sort(), rsort() modifies the original array directly rather than creating a new sorted copy.
Try it yourself
<?php
// Read input
$input = trim(fgets(STDIN));
// Convert JSON string to array
$sales = json_decode($input, true);
// TODO: Write your code below to sort the array in descending order
// Output the sorted array
print_r($sales);
?>This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Advanced Functions
Anonymous FunctionsClosures and 'use'Arrow FunctionsCallback FunctionsUsing 'call_user_func'Variable FunctionsPassing by ReferenceRecursive FunctionsRecap: Function Medley4Multi-dimensional Arrays
Creating a 2D ArrayAccessing 2D Array ElementsModifying 2D Array ElementsIterating with Nested Loops2D Associative ArraysRecap: Simple Grid Exercise2Advanced Array Manipulations
Adding with 'array_push'Removing with 'array_pop'Adding with 'array_unshift'Removing with 'array_shift'Merging Indexed ArraysMerging Associative ArraysExtracting with 'array_slice'Values with 'in_array'Keys with 'array_search'Recap: Playlist Exercise3Sorting Arrays
Sort Indexed Arrays AscendingSort Indexed Arrays DescendingSort Assoc Arrays by ValueSort Assoc Arrays by KeyNatural Order SortingCustom Sorting with 'usort'Recap: Leaderboard Sorting