How to alter sort handler to keep existing order intact in views 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.



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



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

Below is my views configuration:


I am using Drupal 7.41 and views 3.0

Tried so far:

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

        $query-&gt;orderby[1] = array(
             'field' =&gt; 'field_data_field_clinic_hospital_field_clinic_hospital_value',
             'direction' =&gt; 'ASC',
            );
    }
 }


}

But after clicking City head,whole table vanishes.
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:
https://drupal.stackexchange.com/a/125188/22915

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(&amp;$view, &amp;$query) {
  if ($view-&gt;name === 'YOUR_VIEWS_NAME' &amp;&amp; $view-&gt;current_display === 'YOUR_VIEWS_DISPLAY_NAME' &amp;&amp; count($query-&gt;orderby) == 1) {
    $query-&gt;orderby[1] = array(
      'field' =&gt; 'YOUR_SECONDARY_SORT_FIELD',
      'direction' =&gt; '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'.