List View with multiple Sort

In the doc it says we can put an array for the defaultSort: attributes in the config_list.yaml

I’m trying this, but this raises an error

# Default sorting columns
defaultSort:
    column: [level, sort_order]
    direction: [asc, asc]

exception

array_key_exists(): Argument #1 ($key) must be a valid array offset type
~/modules/backend/widgets/lists/HasSorting.php line 178

Hi @chris,

I don’t think it’s possible to pass an array like that.

From what I understand, the array format they refer to is more like a key-value structure for column and direction, like this:

defaultSort:
    column: level
    direction: asc

And if you’re using a string, it would just be defaultSort: level.

Yes, ChatGPT was understanding this differently :slight_smile:
anyway, we can double sort inside the controller itself

public function listExtendQuery($query)
    {
        $query->orderBy('level')->orderBy('sort_order');
    }
````Preformatted text`

@chris I think using listExtendQuery() might potentially conflict with sortable columns if you have them enabled. Since the query ordering would always be applied, it could alter user-initiated column sorting.

Maybe you could consider checking if there’s any active user sorting before applying your default multi-column sort?

If you’re looking for examples of how to detect active sorts/filters in lists, you might find the List Saver plugin by SixGweb helpful. It’s mainly for saving list preferences, but I believe it has some interesting code showing how to access the current list state.

You are absolutely right @apinard
In my case, the other columns do not need to be sorted, so it’s working fine.
thanks for the info