Sorting logic with anonymous function does not work This question is related to my previous question about how to change the sort order in a view. Since this is a new question I thought I better post a new one.

My new problem is that I use hook_views_pre_render() as suggested, but I am having trouble getting the views sort order right. I use an anonymous function like this to change the views order:

/** 
* Implementation of hook_views_pre_render. Sorts the nodes under "Givare"
 */ 
function mysite_views_pre_render(&amp;$view) { 
    if ($view-&gt;name === 'related_items' &amp;&amp; $view-&gt;current_display === 'probes') {   
        $node = node_load(arg(1));
        $sortorder = array();
        $groups = $node-&gt;field_lagg_till_givargrupp['und'];
        foreach($groups as $group){
            $group_nid = $group['nid'];
            $group_node = node_load($group_nid);
            $probe_nids = $group_node-&gt;field_koppla_givare['und'];
            foreach($probe_nids as $probe_nid){
                $sortorder[] = $probe_nid['nid'];
            }
        }

        usort($view-&gt;result, function ($a, $b) use ($sortorder) {
            $cmpa = array_search($a-&gt;nid, $sortorder);
            $cmpb = array_search($b-&gt;nid, $sortorder);  
            dsm($a-&gt;nid." ".$b-&gt;nid." ".$cmpa." ".$cmpb);   
            if($cmpa === $cmpb) {
                return 0;
            } 
            return ($cmpa &lt; $cmpb) ? -1 : 1;
        });
    }
}


The variable $sortorder contains an array with nids in the wanted order. But the resulting $views_result does not follow that order (but it is altered, just in the wrong way). Can anyone see what I am doing wrong?

EDITED: added context – the view in question is a block display with a grid setting (there are several of these block displays in the same view and these block displays are used by Quick Tabs to render each block under a tab as related items.) In this particular block display I have tested to omit exposed filters, to deselect grouping by title etcetera to see if that would impact the sorting, but with no luck. I was surprised that this block needed sorting at all because I give the order explicitely with a contextual filter. See this question for the contextual filter: 
Sort criteria is ignored in Views

Also, as I added the row dsm($a-&gt;nid." ".$b-&gt;nid." ".$cmpa." ".$cmpb); I can see that there are more than one hundred value pairs, but there are only 22 items to be rendered(!). The first few rows of that dsm() looks like this, where for example the nid 20726 is repeated several times:


  20726 20693 7 12
  
  21863 20726 5 7
  
  20726 20695 7 4
  
  20726 20697 7 0
  
  20726 20699 7 15
  
  20861 20726 21 7
  
  20860 20726 20 7
  
  20859 20726 19 7
  
  20858 20726 23 7
  
  20857 20726 22 7
  
  20768 20726 2 7
PROBLEM SOLVED. As J. Reynolds pointed out there were missing nid values in $view->result before the sorting was made.

There was also an issue with the nids because I had forgotten to set the field mapping (migrate d2d) to "do not create automatic URLs" for pathauto during a an earlier migration from D6 to D7 of the group nodes that contained the items in question. So the item nids were fetched from the wrong group entity. My mistake :-(.

But everything is sorted out now. Thanks for your input!