Specifying default sort field / order for List View

Is there a way to specify the default sort field and order for list views. I’m looking for a definition that can be placed in custom vardef, listviewdefs or searchdefs files. Whilst the order is (mostly) remembered, if the ‘Clear Search’ button is clicked prior to a new search it always returns to the system default: date_entered rather than my chosen default.

There seems to be the ability to specify the sort_by and sort_order for subpanels, but nothing for listview, so it seems strange that there isn’t something similar in list view. Following much googling I’ve also tried changing the $_REQUEST using a custom view.list.php file, but this doesn’t work either.

Thanks for your help,

Current Version: 7.6.4

Hello :slight_smile:

I hope I have understood correctly

You are looking to add a default sort inside list view of a module

For example - Contacts

Using 7.9.4

In every module you now have filters

Inside filters there is basic and advanced filter

Click on advanced

At the bottom you will see “save search” - if you save a search you will see “my filters” when next inside the list view

This helps users quickly change between saved filters

Inside the advanced search you can select the Order column by and if it is ascending or descending - see screen shot

I have tried this Advanced Filter in my Support Tickets, with Ascending, by Created Date, and I get the ones at the top that were created a month ago, not the ones created today. :frowning:

If you want to respect user preferences then use the following code to apply default sorting order:

file location: custom/modules/<module_name>/views/view.list.php

public function preDisplay()
   $userPreferenceOrder = $GLOBALS['current_user']->getPreference('listviewOrder', 'AOK_KnowledgeBase2_AOK_KNOWLEDGEBASE'); // you can get required category name from "user_preferences" table. 

	      $_REQUEST['orderBy'] = 'field_name';
	      $_REQUEST['sortOrder'] = 'DESC';