Views custom sorting How can I create a custom sort of items in a view? For example, I want to order content by: (rating * 10) - age in hours. (Rating is 5 star module percentage.)

I've been looking at the Computed Field module and the Views API but I'm not finding what I need.
In the end I created a custom module with a Views Sort handler to write the custom ORDER sql.

function custom_module_views_query_alter(&amp;$view, &amp;$query) {
  if($view-&gt;name === 'viewname') {
    /**
     * Order = (rating * 10) – (hours old / 2)
     * Note: the rating field is divided by 2 because in the DB the rating is out of 100
     */
    $view-&gt;query-&gt;orderby[0]['field'] = "(if(field_data_field_rating.field_rating_rating, field_data_field_rating.field_rating_rating,0) / 2) - ROUND((UNIX_TIMESTAMP(NOW())-created)/60/60/2)";
    $view-&gt;query-&gt;orderby[0]['direction'] = "DESC";

    $view-&gt;query-&gt;orderby[1]['field'] = "created";
    $view-&gt;query-&gt;orderby[1]['direction'] = "DESC";
  }
}


For this to work you either have to add the SQL JOINS in this module or add the rating and posted date fields as a 'sort' in your view.