Is there a way to use sortable() function in laravel filament that can be used to sort posts based on priority?
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Mastering Custom Sorting in Filament: Implementing Priority Sorting via sortable()
As developers working with modern admin panels like Laravel Filament, we often need to move beyond simple ascending or descending sorting. When dealing with custom data attributes—such as priority levels, status codes, or complex enumerated values—the standard orderBy() function falls short. Today, we're diving into how you can leverage the sortable() feature in Filament to implement highly customized sorting logic, specifically for badge columns based on defined priorities.
This guide will show you exactly how to inject a custom comparator function within sortable() to achieve a specific sort order (e.g., High > Medium > Low) without relying on default alphabetical sorting.
The Challenge: Customizing Badge Sorting
In the context of your Filament setup, you have a BadgeColumn displaying a priority field. When a user clicks the sortable arrow on this column, they expect the options to reflect a logical hierarchy (High, Medium, Low) rather than an arbitrary alphabetical order. The default sorting mechanism often treats these strings alphabetically, which is not what we want for business logic.
We need to tell the underlying Eloquent query how to rank these specific values numerically.
The Solution: Using orderByRaw with FIELD()
The key to solving this lies in using a custom closure within the sortable() method that manipulates the database query directly via orderByRaw(). For sorting based on an explicit, predefined order of strings, the SQL function FIELD() is incredibly efficient.
The FIELD(string expression, string sepa, string offset) function returns the position of the value in a list. By defining our priority order explicitly in the list, we dictate the exact sort sequence.
Implementing the Custom Comparator
In your Filament Table definition, you can implement this logic directly within the sortable() method for the specific column:
// Inside your Table class method...
BadgeColumn::make('priority')
->label('Priority')
->colors([
'primary',
'danger' => 'high',
'warning' => 'medium',
'success' => 'low',
])
->sortable(function (Builder $query, string $direction): Builder {
// Define the desired custom sort order: High (1st), Medium (2nd), Low (3rd)
return $query->orderByRaw("FIELD(priority, 'high', 'medium', 'low') ASC");
}),
Code Breakdown and Explanation
sortable(function (Builder $query, string $direction): Builder { ... }): This closure is executed whenever the user attempts to sort the column header. It receives the currentBuilderinstance and the desired sortdirection.orderByRaw(...): Since we are using a custom ranking based on string values rather than simple alphabetical sorting, we must use raw SQL.FIELD(priority, 'high', 'medium', 'low') ASC: This is the magic part. For every row where theprioritycolumn has a value, this expression returns its position in the provided list:- If
priorityis'high',FIELD()returns1. - If
priorityis'medium',FIELD()returns2. - If
priorityis'low',FIELD()returns3.
- If
By ordering by this result ascendingly (ASC), you force the database to sort the records based on the numerical rank (1, 2, 3), effectively sorting 'high' first, then 'medium', and finally 'low'. This ensures the custom priority order is maintained across all sorting interactions within your Filament table.
Best Practices for Custom Sorting in Laravel
When working with complex data structures or custom relationships in Laravel, remember that Eloquent provides powerful tools to structure your queries efficiently. For more intricate sorting requirements, especially those involving multiple fields or dynamic logic, consider utilizing Eloquent Scopes or custom accessor methods. As you build larger applications on the Laravel stack, understanding these underlying mechanisms is crucial for writing clean, performant code. For deeper insights into leveraging the power of Eloquent within your Filament application, exploring resources from laravelcompany.com is highly recommended.
Conclusion
Implementing custom sorting within Filament requires bridging the gap between front-end interaction and back-end database logic. By utilizing sortable() with a carefully constructed orderByRaw expression employing functions like FIELD(), you gain complete control over how your data is presented and sorted in the admin panel. This approach ensures that your application reflects precise business rules, leading to a much more intuitive and functional user experience for anyone managing these records.