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

Render fields url in tpl.php

2  Asked on October 26, 2021 by yoric


How to use Views Page + Attachment to show parent and children nodes?

1  Asked on October 26, 2021 by brian-huether


Hide new topic link based on the forum topics

1  Asked on October 26, 2021 by dominick


set min – max values drupal date_popup timepicker

2  Asked on October 26, 2021 by lorenzo-de-bie


Action links available options / caching

1  Asked on October 26, 2021 by marius-ilie


How to sort the query results?

1  Asked on October 26, 2021 by badm


Alter a path alias depending on a node property

1  Asked on October 26, 2021 by fizzz69


Webform variants as blocks

2  Asked on October 26, 2021 by lordaro


How do I redirect a domain but keep the URL structure?

1  Asked on October 26, 2021 by georgina-smith


How do I translate the weekday in a date?

1  Asked on October 26, 2021 by david-brugmans


Ask a Question

Get help from others!

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