What are some alternative methods to sorting data retrieved from a database in PHP without using the ORDER BY clause?

When sorting data retrieved from a database in PHP without using the ORDER BY clause, one alternative method is to fetch the data into an array and then use PHP's array functions to sort the data based on specific criteria. This allows for more flexibility in sorting options and can be useful when the sorting logic is complex or dynamic.

// Retrieve data from the database
$query = "SELECT * FROM table";
$result = mysqli_query($connection, $query);

// Fetch data into an array
$data = [];
while ($row = mysqli_fetch_assoc($result)) {
    $data[] = $row;
}

// Sort the data based on a specific criteria (e.g. sorting by a specific column)
usort($data, function($a, $b) {
    return $a['column'] <=> $b['column'];
});

// Display the sorted data
foreach ($data as $row) {
    echo $row['column'] . "<br>";
}