How to alter sort handler to keep existing order intact in views

Drupal Answers Asked by Hiranya Sarma on November 18, 2020

I created a views page using foo plugin.When a user visit the page the city and Clinic/Hospital columns by default are being rendered alphabetically (ASC) as in below screenshot.

EDIT: Let’s say for example. City is Bangalore and only for Bangalore Clinic/Hospital(C/H) column is alphabetically ordered(say Hospitals A,B,C,D…).Then user clicks on City head,then for Bangalore C/H column is alphabetically disordered(ex B,D,C,H…) but records are still correct to Bangalore city means those clinic/hospitals are located in Bangalore .Same goes for other cities too.What I want is C/H col should be ordered as earlier (alphabetical) City by City but no matter how city is ordered.

enter image description here

When user clicks on City head (which is sortable),the order of Clinic/Hospital goes heywire (no more alphabetical order) like below image:

enter image description here

Problem: How can I make my Clinic/Hospital order intact even after user clicks on City head.

Below is my views configuration:
enter image description here

I am using Drupal 7.41 and views 3.0

Tried so far:

function mymodule_views_query_alter(&$view, &$query) {
   if($view->name == 'list_of_doctors' && $view->current_display == 'page_1' && count($query->orderby) == 1)

        $query->orderby[1] = array(
             'field' => 'field_data_field_clinic_hospital_field_clinic_hospital_value',
             'direction' => 'ASC',


But after clicking City head,whole table vanishes.

One Answer

As I understand you want to be able to use a secondary sort after overriding the default sort by clicking on a column header.

This can be achieved from the front-end using jQuery plugin TableSorter as described here:

Note this won't work for views with pagination.

It can also be achieved with some custom PHP code. Add the following to a custom module and change the CAPITALIZED words accordingly.

 * Implements hook_views_query_alter().
function YOUR_MODULE_NAME_views_query_alter(&$view, &$query) {
  if ($view->name === 'YOUR_VIEWS_NAME' && $view->current_display === 'YOUR_VIEWS_DISPLAY_NAME' && count($query->orderby) == 1) {
    $query->orderby[1] = array(
      'field' => 'YOUR_SECONDARY_SORT_FIELD',
      'direction' => 'ASC',

This code snippet will make sure that, no matter what column you use as main sorting, you always use a certain field as secondary sort. In your case this would be the Clinic/Hospital field (use system name in code).

Note: if the secondary sort field is not a sortable column, you can't use the query alias. So YOUR_SECONDARY_SORT_FIELD needs to be something like: 'field_data_field_name.field_name_value' instead of 'field_data_field_name_field_name_value'.

Answered by dmsmidt on November 18, 2020

Add your own answers!

Related Questions

File field upload in modal/dialog

1  Asked on December 4, 2020 by ctrnz


How to pre-select items in a select with chosen

1  Asked on December 3, 2020 by jav


Update existing node (not created by Migrate) via Migrate 8.5.x

1  Asked on December 3, 2020 by vaibhav-rana


Secondary Tabs and views

1  Asked on December 2, 2020 by timwhelan


alter Suggestion box for apache solr autosearch

2  Asked on December 1, 2020 by anamika


User specific permission to add links to a menu

2  Asked on November 26, 2020 by mavicc


How to alter sort handler to keep existing order intact in views

1  Asked on November 18, 2020 by hiranya-sarma


Issue using Config Split on content types

0  Asked on November 18, 2020 by vecta


How to create simple confirmation page?

3  Asked on November 11, 2020 by mathewdragon


How do I add a template suggestion for a Views block?

2  Asked on October 21, 2020 by wbeasley


How to programmatically manage taxonomies?

1  Asked on October 15, 2020 by deny-dias


CSS-tags added using Display suite on products disappears

1  Asked on October 4, 2020 by anders-wallenquist


I have issue with commerce_order_load_multiple function

1  Asked on October 1, 2020 by user3686276


Why can’t I install even if I have the required PHP version?

0  Asked on September 13, 2020 by hendrik-kaiser


Ask a Question

Get help from others!

© 2023 All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP