How to make EntityListBuilder sortable? When defining custom content entities one can use EntityListBuilder to build a list of those entities for administrative purpose.

By default such entities are ordered by ID, however it is possible to change ordering by overriding EntityListBuilder::getEntityIds(). Nevertheless this is only ordering of the items. But how to make that table sortable (by clicking on column header to sort ascending and descending)?
This is how i sort my entityListBuilder :

i use the method load() : 

  public function load() {

    $entity_query = \Drupal::service('entity.query')-&gt;get('produits_entity');
    $header = $this-&gt;buildHeader();

    $entity_query-&gt;pager(50);
    $entity_query-&gt;tableSort($header);

    $uids = $entity_query-&gt;execute();

    return $this-&gt;storage-&gt;loadMultiple($uids);
  }


Then i build my header like this :

  /**
   * {@inheritdoc}
   */
  public function buildHeader() {

    $header = array(
        'id' =&gt; array(
            'data' =&gt; $this-&gt;t('N° produit'),
            'field' =&gt; 'id',
            'specifier' =&gt; 'id',
            'class' =&gt; array(RESPONSIVE_PRIORITY_LOW),
        ),
        'nom' =&gt; array(
            'data' =&gt; $this-&gt;t('Nom'),
            'field' =&gt; 'name',
            'specifier' =&gt; 'name',
        ),

        'prixHT' =&gt; array(
            'data' =&gt; $this-&gt;t('Prix HT'),
            'field' =&gt; 'prixHT',
            'specifier' =&gt; 'prixHT',
        ),

        'statut' =&gt; array(
            'data' =&gt; $this-&gt;t('Actif'),
            'field' =&gt; 'actif',
            'specifier' =&gt; 'actif',
        ),

    );

    return $header + parent::buildHeader();
  }


you need to add in your buildHeader() 2 param for each field you want sort :


  name of field and specifier