Search results: order & group by custom field I customized the display of my search results so I can show custom fields in the search results (other question).

I have a custom field ('Department'). I want to show the search results grouped and ordered by that field.
Found an answer... hook_preprocess_search_results is exactly what I need.

function mymodule_preprocess_search_results(&amp;$variables) {
    $variables['search_results'] = '';
    $output_results = '';
    $output = '';

    // loop through results, group by department
    $departments = array();
    foreach ($variables['results'] as $result) {
            $departments[$result['node']-&gt;field_department[$result['node']-&gt;language][0]['value']][] = $result;
    }
    ksort($departments);

    foreach ($departments as $department =&gt; $departmentResults) {
        $value = "";
        foreach ($departmentResults as $key =&gt; $result) {
            $value .= theme('search_result', array('result' =&gt; $result, 'module' =&gt; $variables['module']));
        }

        $output_results .=  '&lt;div id="tabs"&gt;&lt;h3&gt;' . $department . '&lt;/h3&gt;';
        $output_results .=  $value;
        $output_results .=  '&lt;/div&gt;';
    }

    $output .= '&lt;/ul&gt;';
    $output .= $output_results;
    $output .= '&lt;/div&gt;';

    $variables['search_results'] = $output;
}