How to sort View results programmatically in Drupal?

Posted by Jessica Taylor | Updated on

In this answer, I have shared how to sort View results programmatically in Drupal? The views module provides some hooks for ‘external’ manipulations, just like Drupal core.

You can implement hook_views_pre_render(&$view) within a custom module and manipulate the result array available in $view->result:

Alternatively, you could process the view ‘manually’, by copying the behavior of the views_get_view_result() function, but instead of returning the result, you manipulate it and continue to render the view:


Note: This is a lot of code duplication and thus error prone – I do not recommend this and would rather go with the hook implementation above, trying to find a way to get access to your '$important_var' from within that.

If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@] Your question and answer will appear on and help other developers.

Related Questions & Answers