How to adding our own sorting to a view on Drupal? I have develop a drupal view with table format.


Here table rows are Name and Date.
I am sorting a Name column with default sorting in views,it's working fine.
But when user click on views page automatically views page redirected to default sorting in views.
Here problem is when user click on sorting with (asc/desc) that order permanently store in database.


how can we achieve this.
You could alter your view query with hook_views_query_alter to add your custom sorting.

&lt;?php
/**
 * Implementation of hook_views_query_alter
 * @param type $view
 * @param type $query 
 */
function mymodule_views_query_alter(&amp;$view, &amp;$query) {

  //simple example: change the order of the master display
  //if you want to do it only  on a certain display add something 
  // like  "&amp;&amp;$view-&gt;current_display == 'panel_page_1'"
  if ($view-&gt;name == 'VIEWNAME') {

    //to find out what the fieldname ist: use devel and add your desired field as 
    //first filter ( =&gt;orderby[0]). install devel and uncomment the next line
    //dpm($view-&gt;query-&gt;orderby)

    $view-&gt;query-&gt;orderby[0]['field'] = "CASE FIELD_NAME WHEN 'apple' THEN 1 WHEN 'zebra' THEN 2 WHEN 'banna' THEN 3 ELSE 4 END";
  }
}
?&gt;