Fabio_Umpierre's avatar

bug in sorting values ​​in filament table

I have the name column in a filament table with sortable(), but it is sorting wrong example: group 1, group 2, group 3, group 7, group 10

he commands:

group 1, group 10, group 2, group 3, group 7

TextColumn::make('name') ->limit(20) ->searchable() ->sortable() ->label(__('campaign::group.group_name')),

0 likes
3 replies
angeliquevd's avatar
Level 21

This is due to the fact that it is sorted aplhabetically/as a string and not like numbers. One way to fix it is to have leading zeros and therefore group 01, group 02 ...

This can be done automatically with str_pad str_pad($value, 2, '0', STR_PAD_LEFT);

Hope it helps !

1 like
Fabio_Umpierre's avatar

@alainvd First of all, thank you very much for replying my friend. Thanks for your explanation, your solution seems to be better than the one I got just now.

I did it this way:

->sortable(['link'], function ($query, $direction) { return $query->orderByRaw('CAST(SUBSTRING_INDEX(link, " ", -1) AS UNSIGNED) ' . $direction); })

Please or to participate in this conversation.