How can the code be optimized or refactored to improve readability and maintainability while still achieving the desired sorting functionality?
The code can be optimized and refactored by using the built-in PHP function `usort()` to sort the array of objects based on the 'name' property. This will improve readability and maintainability by simplifying the sorting logic and making it more concise.
// Define a custom comparison function for sorting the array of objects by 'name' property
function compareByName($a, $b) {
return strcmp($a->name, $b->name);
}
// Sort the array of objects using the custom comparison function
usort($objects, 'compareByName');