title: Twig extension for sorting Doctrine ArrayCollection
body: <p>I'm trying to write a Twig filter to be able to sort a Doctrine ArrayCollection, but the returned array is not sorted :( Can you please help me to fix this:</p>
  
  <pre><code>class SortExtension extends \Twig_Extension
  { 
  
      public function getFilters()
      {
          return array(
              new \Twig_SimpleFilter('sortby', array($this, 'sortByFilter')),
          );
      }
  
      public function sortbyname( $a, $b )
      {       
          if ($a-&gt;getName() === $b-&gt;getName()) {
              return 0;
          }
          if ( $a-&gt;getName() &lt; $b-&gt;getName() ) {
              return 1;                 
          }
          return -1;
      }
  
      public function sortByFilter($collection)
      {
          $iterator = $collection-&gt;getIterator();
          $iterator-&gt;uasort(array($this, 'sortbyname'));
  
          return $collection;
      } 
  </code></pre>
  
  <p>I'm not quite sure if the returned collection in sortByFilter is changed.</p>