What are the potential challenges of sorting data with non-sequential IDs in PHP?

When sorting data with non-sequential IDs in PHP, a potential challenge is that the built-in sorting functions may not maintain the original order of the data. One way to solve this issue is to create a custom sorting function that sorts the data based on the IDs while preserving the original order.

<?php

// Sample data with non-sequential IDs
$data = [
    5 => 'E',
    3 => 'C',
    1 => 'A',
    4 => 'D',
    2 => 'B'
];

// Custom sorting function based on IDs
uksort($data, function($a, $b) {
    return $a - $b;
});

// Output sorted data
foreach ($data as $id => $value) {
    echo "ID: $id, Value: $value\n";
}

?>