How can I sort object on laravel?

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

How Can I Sort Objects in Laravel? Mastering Sorting Techniques from PHP to Eloquent

As a senior developer, we often deal with data manipulation—sorting, filtering, and transforming collections of objects. When working within the Laravel ecosystem, the way you approach sorting depends heavily on whether you are manipulating raw PHP arrays or interacting with a database via Eloquent.

The issue you encountered stems from mixing procedural PHP array sorting techniques (usort) with object-oriented context, leading to scope errors. While it is possible to sort an array of objects manually in PHP, the idiomatic and most powerful way to handle data sorting in Laravel is by leveraging the power of the database itself through Eloquent.

This post will walk you through both methods: fixing the procedural error and adopting the superior Laravel approach for sorting data.


Understanding the Procedural Challenge with usort

You correctly identified that plain PHP can sort arrays using functions like usort(). Your attempt to define a custom comparison function (sortById) and pass it to usort is conceptually sound, but the error you received—Undefined property ... $sortById—highlights a common pitfall: scope and method binding.

When you call $this->sortById() inside a method where you intend to use it with a global function like usort, PHP struggles to resolve how that method should be executed in that context, especially when dealing with an array of anonymous objects (stdClass).

The Correct Procedural Fix

To make your procedural sorting work correctly, the comparison function passed to usort must be a standalone callable function or an anonymous function (closure) that operates purely on the two arguments provided by usort. It should not rely on object context ($this) unless it is explicitly bound in a way that usort expects.

Here is how you would correctly implement the sorting logic outside of any specific Laravel model context:

// Your data array
$c = [
    (object)['id' => 3, 'name' => 'chelsea.png'],
    (object)['id' => 4, 'name' => 'arsenal.png'],
    (object)['id' => 5, 'name' => 'mu.png'],
    (object)['id' => 1, 'name' => 'city.png'],
];

// Correct implementation using a standalone comparison function
usort($c, function ($a, $b) {
    // Compare based on the 'id' property of the objects
    if ($a->id == $b->id) {
        return 0;
    }
    return ($a->id < $b->id) ? -1 : 1;
});

echo '<pre>' . print_r($c, true) . '</pre>';

This approach works because the callback function passed to usort is executed in a clean context, comparing the properties ($a->id vs $b->id) of the array elements directly.


The Laravel Best Practice: Sorting with Eloquent

While fixing the procedural code solves the immediate problem, relying on manual sorting within application logic for large datasets is inefficient and verbose. In a Laravel environment, data manipulation should be delegated to the database layer whenever possible. This is where Eloquent shines.

Instead of fetching all records and sorting them in PHP memory, you instruct the database (which is optimized for this task) to return the results already sorted. This significantly reduces application overhead and improves performance, especially as your dataset grows.

To sort data fetched via Eloquent, you use the orderBy() method on your query builder.

Example: Sorting Eloquent Collections

If you were fetching these records from a database table mapped to an Eloquent model (e.g., CityModel), sorting by ID becomes incredibly simple and efficient:

use App\Models\CityModel;

// 1. Fetch data sorted by 'id' ascending (default)
$cities = CityModel::orderBy('id')->get();

// If you needed descending order, you would use:
// $cities = CityModel::orderBy('id', 'desc')->get();

// Output: The $cities collection is now perfectly ordered based on the database index.

This approach is significantly cleaner and adheres to the principles of modern application development. It allows Laravel to handle the heavy lifting, ensuring that your application remains fast and maintainable. For robust data management in Laravel projects, always prioritize Eloquent methods over manual array manipulation when dealing with persistence concerns like sorting.


Conclusion

To summarize, while you can manually sort an array of objects using usort by writing a custom comparison function, this method is cumbersome and error-prone in complex applications. For any data sourced from a database—which is the standard practice in Laravel development—the superior solution is to use Eloquent's querying capabilities. By employing methods like orderBy(), you delegate the sorting task to the highly optimized database engine, resulting in code that is cleaner, faster, and more maintainable. Always strive to use the tools provided by the framework to achieve optimal results.