title: PHP Iterator Custom Sort Order (Laravel 4)
body: <p>I have a "Post" object, accessed via the IOC container.  Various errors tell me this object's type ends up as a "<a href="http://laravel.com/api/class-Illuminate.Support.Collection.html" rel="nofollow noreferrer">Collection</a>", which implements several interfaces, including IteratorAggregate and ArrayAccess.</p>
  
  <p>I want to display a user-defined group of posts according to a specific order, e.g.:</p>
  
  <pre><code>$desired=array("69","63","70");//these represent post id's
  </code></pre>
  
  <p>Just sorting an array in this manner seems complex, but I want to sort my collection.  I have been researching various combinations of usort(), uksort(), Eloquent's sortBy(), array_multisort()... but the most obvious solutions result in orders like 3,2,1 or 1,2,3, not 2,1,3.  </p>
  
  <p>The closest I have gotten to this goal is to fetch the ones I want, </p>
  
  <pre><code>//BlogController
  private function myposts($desired){
      $posts=$this-&gt;post-&gt;whereIn('id',$desired)-&gt;get();
      ...
  </code></pre>
  
  <p>"convert" the Collection object to an array, </p>
  
  <pre><code>$posts=$posts-&gt;toArray();
  </code></pre>
  
  <p>and treat the array with a custom function: <a href="https://stackoverflow.com/questions/348410/sort-an-array-based-on-another-array">source</a></p>
  
  <pre><code>function sortArrayByArray($array,$orderArray) {
      $ordered = array();
      foreach($orderArray as $key) {
          if(array_key_exists($key,$array)) {
              $ordered[$key] = $array[$key];
              unset($array[$key]);
          }
      }
      return $ordered + $array;
  }
  $sorted=sortArrayByArray($array1,$desired);
  </code></pre>
  
  <p>I can then vardump the array in the correct order, but since it is now an array, I can't access the $posts object in my view.  Can I convert the array back into a post object?</p>
  
  <p>This whole approach feels wasteful anyway, converting to an array and back... is it?  Is there a more straightforward way of sorting the contents of a "Collection"?</p>
  
  <p>This is a little better, perhaps (a native php function):</p>
  
  <pre><code>array_multisort($desired,$posts,SORT_STRING);
  //only works with string keys
  //$desire=(1,2,3) will not work!
  </code></pre>
  
  <p>Again, this works for arrays, but attempting directly on the "posts" object fails...</p>
  
  <p>Finally, I discovered using a Presenter: <a href="https://github.com/robclancy/presenter#array-usage" rel="nofollow noreferrer">https://github.com/robclancy/presenter#array-usage</a> which works in the view, after one of the above is completed in the controller:</p>
  
  <pre><code>@foreach($posts as $post)
  &lt;?php $post=new PostPresenter($post) ?&gt;
  {{View::make('site.post.article')-&gt;with(compact('post'))}}
  @endforeach
  </code></pre>
  
  <p>This finally works, but it still feels like a long way to do it.  Is there a better way to accomplish this task?  Performance concerns or best practices with one method vs. another?  Thanks in advance for anyone able to help.</p>