06: Type-Safe Sorting with SortDirection
Overview
Section titled “Overview”Give a leaderboard function a type-safe way to accept a sort direction. Callers choose a case from the built-in enum.
Prerequisites
Section titled “Prerequisites”- PHP 8.6 CLI. Confirm the version with
php --version. - Review the previous chapter if you’re following the series in order.
Allow about 10 minutes for the walkthrough and exercise.
What You’ll Build
Section titled “What You’ll Build”A runnable sort-direction.php example.
Step 1: Run the example (~5 min)
Section titled “Step 1: Run the example (~5 min)”Run the following from the root of the Code with PHP repository.
php code/php-8-6/06-sort-direction/sort-direction.php<?php
declare(strict_types=1);
function orderScores(array $scores, SortDirection $direction = SortDirection::Ascending): array{ uasort($scores, static fn (int $left, int $right): int => match ($direction) { SortDirection::Ascending => $left <=> $right, SortDirection::Descending => $right <=> $left, }); return $scores;}
$scores = ['Ari' => 42, 'Jo' => 60];echo implode(', ', array_keys(orderScores($scores))), PHP_EOL;echo implode(', ', array_keys(orderScores($scores, SortDirection::Descending))), PHP_EOL;Expected Result
Section titled “Expected Result”Ari, JoJo, AriWhy It Works
Section titled “Why It Works”The signature accepts Ascending and Descending through a shared enum type. The sample maps the case to a comparator because uasort() expects a comparator. It preserves the player names as array keys.
Exercise
Section titled “Exercise”Add Cara with 65 points. Confirm that she appears first only in descending order.
Wrap-up
Section titled “Wrap-up”Continue with DocComments on Function Parameters.