title: How to sort a table by object/tablehead in Twig/Symfony2
body: <p>I am working on a project with a table generated from crud command. See my code below from my "Voorraad" entity</p>
  
  <p>index.html.twig</p>
  
  <pre><code>{% extends '::base.html.twig' %}
  
  {% block body -%}
  &lt;h1&gt;Voorraad lijst&lt;/h1&gt;
  
  &lt;table class="records_list"&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;!-- &lt;th&gt;Id&lt;/th&gt; --&gt;
              &lt;th&gt;Product&lt;/th&gt;
              &lt;th&gt;Type&lt;/th&gt;
              &lt;th&gt;Fabriek&lt;/th&gt;
              &lt;th&gt;Inkoopprijs&lt;/th&gt;
              &lt;th&gt;Verkoopprijs
              &lt;th&gt;Aantal&lt;/th&gt;
              &lt;th&gt;Locatie&lt;/th&gt;
              &lt;th&gt;Actions&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
      {% for entity in entities %}
          &lt;tr&gt;
     &lt;!--         &lt;td&gt;&lt;a href="{{ path('voorraad_show', { 'id': entity.id }) }}"&gt;{{ entity.id }}&lt;/a&gt;&lt;/td&gt; --&gt;
              &lt;td&gt;{{ entity.getProduct().getNaam() }}&lt;/td&gt;
              &lt;td&gt;{{ entity.getProduct().getType() }}&lt;/td&gt;
              &lt;td&gt;{{ entity.getProduct().getFabriek() }}&lt;/td&gt;
              &lt;td&gt;{{ entity.getProduct().getInkoopprijs() }}&lt;/td&gt;
              &lt;td&gt;{{ entity.getProduct().getVerkoopprijs() }}&lt;/td&gt;
              &lt;td&gt;{{ entity.aantal }}&lt;/td&gt;
              &lt;td&gt;{{ entity.getLocatie().getLocatienaam() }}&lt;/td&gt;
              &lt;td&gt;
  
                      &lt;a href="{{ path('voorraad_edit', { 'id': entity.id }) }}"&gt;Voorraad aanpassen&lt;/a&gt;
  
              &lt;/td&gt;
          &lt;/tr&gt;
      {% endfor %}
      &lt;/tbody&gt;
  &lt;/table&gt;
  
      &lt;ul&gt;
      &lt;li&gt;
          &lt;a href="{{ path('voorraad_new') }}"&gt;
              Nieuwe voorraad toevoegen   
          &lt;/a&gt;
      &lt;/li&gt;
  &lt;/ul&gt;
  {% endblock %}
  </code></pre>
  
  <p>Controller</p>
  
  <pre><code>&lt;?php
  
  namespace ToolsForEver\VoorraadBundle\Controller;
  
  use Symfony\Component\HttpFoundation\Request;
  use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
  use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  use ToolsForEver\VoorraadBundle\Entity\Voorraad;
  use ToolsForEver\VoorraadBundle\Form\VoorraadType;
  
  /**
   * Voorraad controller.
   *
   * @Route("/voorraad")
   */
  class VoorraadController extends Controller
  {
  
  /**
   * Lists all Voorraad entities.
   *
   * @Route("/", name="voorraad")
   * @Method("GET")
   * @Template()
   */
  public function indexAction()
  {
      $em = $this-&gt;getDoctrine()-&gt;getManager();
  
      $entities = $em-&gt;getRepository('ToolsForEverVoorraadBundle:Voorraad')-&gt;findAll();
  
      return array(
          'entities' =&gt; $entities,
      );
  }
  /**
   * Creates a new Voorraad entity.
   *
   * @Route("/", name="voorraad_create")
   * @Method("POST")
   * @Template("ToolsForEverVoorraadBundle:Voorraad:new.html.twig")
   */
  public function createAction(Request $request)
  {
      $entity = new Voorraad();
      $form = $this-&gt;createCreateForm($entity);
      $form-&gt;handleRequest($request);
  
      if ($form-&gt;isValid()) {
          $em = $this-&gt;getDoctrine()-&gt;getManager();
          $em-&gt;persist($entity);
          $em-&gt;flush();
  
          return $this-&gt;redirect($this-&gt;generateUrl('voorraad_show', array('id' =&gt; $entity-&gt;getId())));
      }
  
      return array(
          'entity' =&gt; $entity,
          'form'   =&gt; $form-&gt;createView(),
      );
  }
  
  /**
   * Creates a form to create a Voorraad entity.
   *
   * @param Voorraad $entity The entity
   *
   * @return \Symfony\Component\Form\Form The form
   */
  private function createCreateForm(Voorraad $entity)
  {
      $form = $this-&gt;createForm(new VoorraadType(), $entity, array(
          'action' =&gt; $this-&gt;generateUrl('voorraad_create'),
          'method' =&gt; 'POST',
      ));
  
      $form-&gt;add('submit', 'submit', array('label' =&gt; 'Create'));
  
      return $form;
  }
  
  /**
   * Displays a form to create a new Voorraad entity.
   *
   * @Route("/new", name="voorraad_new")
   * @Method("GET")
   * @Template()
   */
  public function newAction()
  {
      $entity = new Voorraad();
      $form   = $this-&gt;createCreateForm($entity);
  
      return array(
          'entity' =&gt; $entity,
          'form'   =&gt; $form-&gt;createView(),
      );
  }
  
  /**
   * Finds and displays a Voorraad entity.
   *
   * @Route("/{id}", name="voorraad_show")
   * @Method("GET")
   * @Template()
   */
  public function showAction($id)
  {
      $em = $this-&gt;getDoctrine()-&gt;getManager();
  
      $entity = $em-&gt;getRepository('ToolsForEverVoorraadBundle:Voorraad')-&gt;find($id);
  
      if (!$entity) {
          throw $this-&gt;createNotFoundException('Unable to find Voorraad entity.');
      }
  
      $deleteForm = $this-&gt;createDeleteForm($id);
  
      return array(
          'entity'      =&gt; $entity,
          'delete_form' =&gt; $deleteForm-&gt;createView(),
      );
  }
  
  /**
   * Displays a form to edit an existing Voorraad entity.
   *
   * @Route("/{id}/edit", name="voorraad_edit")
   * @Method("GET")
   * @Template()
   */
  public function editAction($id)
  {
      $em = $this-&gt;getDoctrine()-&gt;getManager();
  
      $entity = $em-&gt;getRepository('ToolsForEverVoorraadBundle:Voorraad')-&gt;find($id);
  
      if (!$entity) {
          throw $this-&gt;createNotFoundException('Unable to find Voorraad entity.');
      }
  
      $editForm = $this-&gt;createEditForm($entity);
      $deleteForm = $this-&gt;createDeleteForm($id);
  
      return array(
          'entity'      =&gt; $entity,
          'edit_form'   =&gt; $editForm-&gt;createView(),
          'delete_form' =&gt; $deleteForm-&gt;createView(),
      );
  }
  
  /**
  * Creates a form to edit a Voorraad entity.
  *
  * @param Voorraad $entity The entity
  *
  * @return \Symfony\Component\Form\Form The form
  */
  private function createEditForm(Voorraad $entity)
  {
      $form = $this-&gt;createForm(new VoorraadType(), $entity, array(
          'action' =&gt; $this-&gt;generateUrl('voorraad_update', array('id' =&gt; $entity-&gt;getId())),
          'method' =&gt; 'PUT',
      ));
  
      $form-&gt;add('submit', 'submit', array('label' =&gt; 'Update'));
  
      return $form;
  }
  /**
   * Edits an existing Voorraad entity.
   *
   * @Route("/{id}", name="voorraad_update")
   * @Method("PUT")
   * @Template("ToolsForEverVoorraadBundle:Voorraad:edit.html.twig")
   */
  public function updateAction(Request $request, $id)
  {
      $em = $this-&gt;getDoctrine()-&gt;getManager();
  
      $entity = $em-&gt;getRepository('ToolsForEverVoorraadBundle:Voorraad')-&gt;find($id);
  
      if (!$entity) {
          throw $this-&gt;createNotFoundException('Unable to find Voorraad entity.');
      }
  
      $deleteForm = $this-&gt;createDeleteForm($id);
      $editForm = $this-&gt;createEditForm($entity);
      $editForm-&gt;handleRequest($request);
  
      if ($editForm-&gt;isValid()) {
          $em-&gt;flush();
  
          return $this-&gt;redirect($this-&gt;generateUrl('voorraad_edit', array('id' =&gt; $id)));
      }
  
      return array(
          'entity'      =&gt; $entity,
          'edit_form'   =&gt; $editForm-&gt;createView(),
          'delete_form' =&gt; $deleteForm-&gt;createView(),
      );
  }
  /**
   * Deletes a Voorraad entity.
   *
   * @Route("/{id}", name="voorraad_delete")
   * @Method("DELETE")
   */
  public function deleteAction(Request $request, $id)
  {
      $form = $this-&gt;createDeleteForm($id);
      $form-&gt;handleRequest($request);
  
      if ($form-&gt;isValid()) {
          $em = $this-&gt;getDoctrine()-&gt;getManager();
          $entity = $em-&gt;getRepository('ToolsForEverVoorraadBundle:Voorraad')-&gt;find($id);
  
          if (!$entity) {
              throw $this-&gt;createNotFoundException('Unable to find Voorraad entity.');
          }
  
          $em-&gt;remove($entity);
          $em-&gt;flush();
      }
  
      return $this-&gt;redirect($this-&gt;generateUrl('voorraad'));
  }
  
  /**
   * Creates a form to delete a Voorraad entity by id.
   *
   * @param mixed $id The entity id
   *
   * @return \Symfony\Component\Form\Form The form
   */
  private function createDeleteForm($id)
  {
      return $this-&gt;createFormBuilder()
          -&gt;setAction($this-&gt;generateUrl('voorraad_delete', array('id' =&gt; $id)))
          -&gt;setMethod('DELETE')
          -&gt;add('submit', 'submit', array('label' =&gt; 'Delete'))
          -&gt;getForm()
      ;
  }
  }
  </code></pre>
  
  <p>I would like to order the table by "Locatie" (=Location), the following tablehead.</p>
  
  <pre><code>&lt;th&gt;Locatie&lt;/th&gt;
  </code></pre>
  
  <p>Locatie is a foreign key of the entity "Voorraad", and primary key of the entity "Locatie". I also have a entitiy called "Product". Unfortunately I do not have any idea how to get this done..</p>