Sort matrix blocks by date and limit to 10 entries I'm loading multiple matrix blocks from multiple entries to the homepage if the date is today or in the future. So far this works but now I'm trying to sort the blocks by date and limit the output to 10 dates.

Here's the code I'm currently using:

{% for entry in craft.entries.section('projects') %}

  {% for block in entry.agendav2 %}

    {% if block.Datum &gt;= now %}

      {% switch block.type %}

        {% case 'Datum' %}

          &lt;table class="agenda_item"&gt;
            &lt;tr&gt;
              &lt;td class="table_title performance-date table_col-2-3"&gt;{{ block.Datum|date("d.m.Y") }}&lt;/th&gt;
                &lt;td class="table_title event-location table_col-1-3"&gt;{{ block.locatie }}&lt;/th&gt;
              &lt;/tr&gt;
            &lt;tr&gt;
              &lt;td class="performance-title table_col-2-3"&gt;&lt;a href="{{ entry.url }}"&gt;{{ entry.globalTitle }}&lt;/a&gt;&lt;/td&gt;
              &lt;td class="event-title table_col-1-3"&gt;&lt;a target="_blank" href="{{ block.eventUrl }}"&gt;{{ block.evenement }}&lt;/a&gt;&lt;/td&gt;
            &lt;/tr&gt;
          &lt;/table&gt;

      {% endswitch %}

    {% endif %}   

  {% endfor %}  

{% endfor %}


Right now it's loading every upcoming date per entry like:


Entry 1: 31-01-2018 
Entry 1: 01-02-2018
Entry 1: 02-03-2018
Entry 2: 30-01-2018
Entry 2: 06-02-2018
Entry 2: 03-03-2018
Entry 3: 23-01-2018
Entry 3: 25-01-2018
Entry 3: 08-03-2018
Your entry.agendav2 is not an array of elements but a ElementCriteriaModel so you can include both properties in your query 

{% for block in entry.agendav2.limit(10).order('Datum') %}


That's basically it. 

Edit: you can search for entries in the future with 

 Datum('&gt;= ' ~ 'now'|date('c')) 


Like stated here https://craftcms.stackexchange.com/a/23697/5557

Edit:

After we discussed the issue in my comments I came up with this solution

{% set matrixBlocks = craft.yourPluginHandle.getSortedMatrixBlocks() %}
{% for matrix in matrixBlocks %}
    ...
    {{ matrix.datum }}
    ...
{% endfor %}


And insert this in your PluginVariable

public function getSortedMatrixBlocks(){
    // the id of your matrix field
    $matrixFieldId = 61;

    // check your database for your matrix field table and insert your Datum field column
    // it will be a string like field_{blockHandle}_{fieldHandle}
    // for me it is
    $matrixFieldDatum = 'field_test_datum';

    // lowercase handle of your field
    $matrixFieldHandle = 'matrixfield';
    $matrixIds = craft()-&gt;db-&gt;createCommand()
        -&gt;select('craft_matrixblocks.id')
        -&gt;from('matrixblocks')
        -&gt;where('craft_matrixblocks.fieldId = :fieldId', ['fieldId' =&gt; $matrixFieldId])
        -&gt;limit(10)
        -&gt;join('matrixcontent_'.$matrixFieldHandle, 'craft_matrixblocks.id = craft_matrixcontent_' . $matrixFieldHandle . '.elementId')
        -&gt;order($matrixFieldDatum . ' ASC')
        -&gt;queryColumn();

    $allMatrixBlocks = [];
    foreach ($matrixIds as $id){
        $allMatrixBlocks[] = craft()-&gt;matrix-&gt;getBlockById($id);
    }

    return $allMatrixBlocks;
}