The SortDirection enum

Introduction

The SortDirection enum is used to specify the direction in which values are sorted.

Note: As of PHP 8.6.0, none of the functions bundled with PHP accept a SortDirection. For example, array_multisort still expects SORT_ASC or SORT_DESC.

Enum synopsis

SortDirection Ascending Descending

Examples

Example #1 Sorting names with SortDirection

<?php

function sortNames(array $names, SortDirection $direction): array
{
    match ($direction) {
        SortDirection::Ascending => sort($names),
        SortDirection::Descending => rsort($names),
    };

    return $names;
}

$names = ['Charlie', 'Alice', 'Bob'];

print_r(sortNames($names, SortDirection::Descending));

The above example will output:

Array
(
    [0] => Charlie
    [1] => Bob
    [2] => Alice
)