What potential pitfalls should be considered when sorting temperature values in PHP arrays?

When sorting temperature values in PHP arrays, one potential pitfall to consider is that temperatures are typically represented as strings (e.g., "25°C"), which can lead to unexpected sorting results. To avoid this issue, it's important to extract the numerical value from the temperature string before sorting the array. This can be done by using a custom sorting function that converts the temperature strings to numbers for comparison.

// Sample array of temperature values
$temperatures = ["25°C", "18°C", "30°C", "22°C"];

// Custom sorting function to extract numerical value from temperature strings
function compareTemperatures($a, $b) {
    $tempA = intval($a); // Extract numerical value from temperature string
    $tempB = intval($b); // Extract numerical value from temperature string
    
    if ($tempA == $tempB) {
        return 0;
    }
    return ($tempA < $tempB) ? -1 : 1;
}

// Sort the temperatures array using the custom sorting function
usort($temperatures, "compareTemperatures");

// Output sorted temperatures
print_r($temperatures);