What is the best way to sort an array of ranks in PHP in descending order based on a predefined hierarchy?

When sorting an array of ranks in PHP in descending order based on a predefined hierarchy, you can use the `usort` function along with a custom comparison function. The custom comparison function should compare the ranks based on the predefined hierarchy and return -1, 0, or 1 accordingly. This will ensure that the array is sorted in the desired order.

// Define the predefined hierarchy
$hierarchy = ['A', 'B', 'C', 'D', 'E'];

// Sample array of ranks
$ranks = ['D', 'A', 'B', 'C', 'E'];

// Custom comparison function based on hierarchy
function customSort($a, $b) {
    global $hierarchy;
    return array_search($b, $hierarchy) - array_search($a, $hierarchy);
}

// Sort the array in descending order based on hierarchy
usort($ranks, 'customSort');

// Output the sorted array
print_r($ranks);