What are some common methods for sorting data in a PHP application, particularly when retrieving data from a database?

When retrieving data from a database in a PHP application, it is common to need to sort the data based on specific criteria. One common method for sorting data is to use the ORDER BY clause in SQL queries when fetching data from the database. This allows you to specify the column by which you want to order the results, as well as the order (ASC for ascending or DESC for descending).

// Example SQL query to retrieve data from a database table and sort it by a specific column
$query = "SELECT * FROM users ORDER BY username ASC";
$result = mysqli_query($connection, $query);

// Fetch and display the sorted data
while ($row = mysqli_fetch_assoc($result)) {
    echo $row['username'] . "<br>";
}