Sort array of objects by one property

Sort array of objects by one property

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Sorting Arrays of Objects in PHP

Sorting an array of objects is a fundamental operation, yet the approach you take often depends entirely on your data structure and the environment you are working in. The specific example provided involves stdClass objects populated within a standard PHP array. To sort this collection by a property like name or count, you generally rely on built-in sorting functions that accept a custom comparison logic.

The most direct method for this scenario is using usort(). This function requires a user-defined comparison function that accepts two arguments, $a and $b, representing the objects being compared. Inside this callback, you access the specific property—say, name—and compare their string values. If sorting numerically by count, you simply cast those properties to integers before comparing.

$items = [
    (object)['ID' => 1, 'name' => 'Mary Jane', 'count' => 420],
    (object)['ID' => 2, 'name' => 'Johnny', 'count' => 234],
    (object)['ID' => 3, 'name' => 'Kathy', 'count' => 4354],
];

usort($items, function ($a, $b) {
    // Ascending order by name
    return strcmp($a->name, $b->name);
});

Using strcmp ensures case-sensitive alphabetical sorting. If you need case-insensitive behavior, switch to strcoll with a locale or use strcasecmp. For numeric properties like count, the logic changes slightly. A simple subtraction ($a->count - $b->count) works for integers but can lead to overflow issues if the difference exceeds the maximum integer size. A safer approach is using spaceship operators (available in PHP 7+) or explicit comparison functions.

usort($items, function ($a, $b) {
    // Ascending order by count
    return $a->count <=> $b->count;
});

While usort() is robust and flexible, it operates in-place, meaning the original array reference is modified. This can be problematic if you need to preserve the original data structure for subsequent operations or rendering. In such cases, wrapping the array in an anonymous function before passing it to usort creates a copy, leaving the original untouched.

$sortedItems = $items; // Creates a shallow copy
usort($sortedItems, ...);

Alternatively, PHP 8 introduced array_sort() which is a dedicated function for this exact purpose, though it behaves similarly to usort(). It offers slightly cleaner syntax but doesn't fundamentally change the underlying sorting algorithm or memory implications.

Performance and Stability Considerations

When choosing between usort() and other sorting mechanisms, stability matters. Stable sort algorithms maintain the relative order of records with equal keys. PHP's internal sorting implementation (often a hybrid of quicksort, heapsort, and introsort) is not guaranteed to be stable in all contexts, though modern implementations tend to preserve order for identical elements. If strict stability is required—for instance, ensuring secondary sort criteria aren't disrupted—you might consider using uasort() if you need to maintain key associations, or even resorting to external libraries that guarantee stability.

In high-performance environments where sorting massive datasets repeatedly, the overhead of creating closure objects in PHP can become a bottleneck. Profiling your specific use case is essential before optimizing for micro-seconds. Often, the simplest solution using native PHP functions provides sufficient performance without introducing unnecessary complexity or dependencies.

If you are working within a larger Laravel application and need more complex sorting logic involving multiple properties or database-driven data, checking resources like LaravelCompany can provide deeper insights into leveraging Eloquent scopes or collection methods that abstract these details away from raw PHP arrays.

Our network