Skip to content

feat: add sortUsing function to column for custom sorting#2066

Open
xabec wants to merge 2 commits intoPower-Components:6.xfrom
xabec:6.x
Open

feat: add sortUsing function to column for custom sorting#2066
xabec wants to merge 2 commits intoPower-Components:6.xfrom
xabec:6.x

Conversation

@xabec
Copy link

@xabec xabec commented Jan 21, 2026

⚡ PowerGrid - Pull Request

  • Bug fix
  • Enhancement
  • New feature
  • Breaking change

Description

Allows sorting columns by a callback giving flexibility to create completely custom sorts.

Examples of what can you do with it:

            Column::make('Price', 'price')
                ->sortUsing(function ($query, string $direction) {
                    // Custom sort: sort by price, but nulls go last
                    $query->orderByRaw("price IS NULL, price {$direction}");
                }),
            Column::make('Live Value ('.$currencySymbol.')', 'live_value')
                ->sortUsing(function ($query, $direction) {
                    $query->selectRaw('account_balances.*, (
                        SELECT cp.price * account_balances.amount
                        FROM cryptos c
                        JOIN crypto_prices cp ON cp.crypto_id = c.id
                        WHERE c.provider_id = account_balances.asset_id
                        ORDER BY cp.created_at DESC
                        LIMIT 1
                    ) AS live_value_computed')
                        ->orderBy('live_value_computed', $direction);
                }),
            Column::make('Calories', 'calories')
                ->sortUsing(function (Collection $collection, string $direction) {
                    // Custom sort: sort by calories in groups (low/medium/high)
                    return $collection->sortBy(function ($item) {
                        $calories = data_get($item, 'calories');
                        if ($calories <= 200) {
                            return 1; // Low
                        }
                        if ($calories <= 400) {
                            return 2; // Medium
                        }

                        return 3; // High
                    }, SORT_REGULAR, $direction === 'desc');
                }),

Related Issue(s):

#1850

Documentation

This PR requires Documentation update?

  • Yes
  • No
  • I have already submitted a Documentation pull request.

@xabec
Copy link
Author

xabec commented Jan 30, 2026

So can we merge this @luanfreitasdev ?

@luanfreitasdev
Copy link
Member

Hi @xabec, thank you for that! I need to test it and add it to the documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants