<p>Your <code>entry.agendav2</code> is not an array of elements but a <a href="https://craftcms.com/docs/templating/elementcriteriamodel" rel="nofollow noreferrer">ElementCriteriaModel</a> so you can include both properties in your query </p>

<pre><code>{% for block in entry.agendav2.limit(10).order('Datum') %}
</code></pre>

<p>That's basically it. </p>

<p>Edit: you can search for entries in the future with </p>

<pre><code> Datum('&gt;= ' ~ 'now'|date('c')) 
</code></pre>

<p>Like stated here <a href="https://craftcms.stackexchange.com/a/23697/5557">https://craftcms.stackexchange.com/a/23697/5557</a></p>

<p><strong>Edit</strong>:</p>

<p>After we discussed the issue in my comments I came up with this solution</p>

<pre><code>{% set matrixBlocks = craft.yourPluginHandle.getSortedMatrixBlocks() %}
{% for matrix in matrixBlocks %}
    ...
    {{ matrix.datum }}
    ...
{% endfor %}
</code></pre>

<p>And insert this in your PluginVariable</p>

<pre><code>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;
}
</code></pre>

CC BY-SA 3.0
2018-02-01T10:03:35.257Z
Tried this but still it only sorts it per entry instead of mixing up the entries per date. This is what I have right now: http://sandmandance.com/dev/
Christian Lemmers
CC BY-SA 3.0
2018-02-01T10:16:38.840Z
what do you mean with "It sorts it per entry"? You have to make sure you selected the correct field with the correct handle, I just used the code you provided
Robin Schambach
CC BY-SA 3.0
2018-02-06T10:00:06.940Z
Ok, let me try to explain myself a little bit more: 
I've got a (channel)section named 'Projects' which has multiple entries. In those entries you can add multiple tour dates to the particular entry using a Matrix Field. This will result in a complete tour schedule per entry. Now I'd like to combine all these upcoming tour dates for all these entries and display the upcoming tour dates on the homepage in chronological order. Only the problem is, right now it finds the first entry, sort the upcoming dates of that particular entry and moves on to the next one.
Christian Lemmers
CC BY-SA 3.0
2018-02-06T10:10:23.723Z
Alright I see it now. Do you already have a project plugin or do you prefer to do everything in twig?
Robin Schambach
CC BY-SA 3.0
2018-02-06T10:23:49.180Z
As for now I was trying to do it all in twig, but if there's a easy plugin solution for it I'm more than willing to give that a shot.
Christian Lemmers
CC BY-SA 3.0
2018-02-06T10:36:12.243Z
I'm talking about creating a plugin because it's going to be super slow and messy with twig
Robin Schambach
CC BY-SA 3.0
2018-02-06T10:43:46.713Z
I'm not really skilled to create a plugin from scratch. But is it really necessary to create a plugin? It seems like a really basic function to me.
Christian Lemmers
CC BY-SA 3.0
2018-02-06T10:54:07.897Z
No it is not because you can't revert search matrix blocks in Craft. So you'll have to loop every entry in order to fetch all of your matrix blocks and sort them afterwards somehow. Twig is not supposed to contain business logic, it's only for rendering elements and sorting arrays is a mess. Creating a Plugin is super easy with pluginfactory.io. If you don't want to do it in php, you can wait for someone else to answer
Robin Schambach
CC BY-SA 3.0
2018-02-07T14:22:38.650Z
Yeah, guess I've gotta wait for someone with a different solution.
Christian Lemmers
CC BY-SA 3.0
2018-01-31T16:45:50.470Z
2018-02-06T11:18:51.040Z
2018-02-06T11:18:51.040Z
Robin Schambach
<p>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.</p>

<p>Here's the code I'm currently using:</p>

<pre><code>{% 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 %}
</code></pre>

<p>Right now it's loading every upcoming date per entry like:</p>

<ul>
<li>Entry 1: 31-01-2018 </li>
<li>Entry 1: 01-02-2018</li>
<li>Entry 1: 02-03-2018</li>
<li>Entry 2: 30-01-2018</li>
<li>Entry 2: 06-02-2018</li>
<li>Entry 2: 03-03-2018</li>
<li>Entry 3: 23-01-2018</li>
<li>Entry 3: 25-01-2018</li>
<li>Entry 3: 08-03-2018</li>
</ul>

<ordering>
Sort matrix blocks by date and limit to 10 entries
# Sort matrix blocks by date and limit to 10 entries

(Asked by: username_0 on 2018-01-31)

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 >= now %}

      {% switch block.type %}

        {% case 'Datum' %}

          
            
              {{ block.Datum|date("d.m.Y") }}
                {{ block.locatie }}
              
            
              {{ entry.globalTitle }}
              {{ block.evenement }}
            
          

      {% 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

**Tags:** ordering


---

## 1 answers

### Answer by username_1 on 2018-01-31. Score: 3

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('>= ' ~ '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()->db->createCommand()
        ->select('craft_matrixblocks.id')
        ->from('matrixblocks')
        ->where('craft_matrixblocks.fieldId = :fieldId', ['fieldId' => $matrixFieldId])
        ->limit(10)
        ->join('matrixcontent_'.$matrixFieldHandle, 'craft_matrixblocks.id = craft_matrixcontent_' . $matrixFieldHandle . '.elementId')
        ->order($matrixFieldDatum . ' ASC')
        ->queryColumn();

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

    return $allMatrixBlocks;
}

> **Comments:**
> **username_0**: Tried this but still it only sorts it per entry instead of mixing up the entries per date. This is what I have right now: http://sandmandance.com/dev/  
> **username_1**: what do you mean with "It sorts it per entry"? You have to make sure you selected the correct field with the correct handle, I just used the code you provided  
> **username_0**: Ok, let me try to explain myself a little bit more:  I've got a (channel)section named 'Projects' which has multiple entries. In those entries you can add multiple tour dates to the particular entry using a Matrix Field. This will result in a complete tour schedule per entry. Now I'd like to combine all these upcoming tour dates for all these entries and display the upcoming tour dates on the homepage in chronological order. Only the problem is, right now it finds the first entry, sort the upcoming dates of that particular entry and moves on to the next one.  
> **username_1**: Alright I see it now. Do you already have a project plugin or do you prefer to do everything in twig?  
> **username_0**: As for now I was trying to do it all in twig, but if there's a easy plugin solution for it I'm more than willing to give that a shot.  
> **username_1**: I'm talking about creating a plugin because it's going to be super slow and messy with twig  
> **username_0**: I'm not really skilled to create a plugin from scratch. But is it really necessary to create a plugin? It seems like a really basic function to me.  
> **username_1**: No it is not because you can't revert search matrix blocks in Craft. So you'll have to loop every entry in order to fetch all of your matrix blocks and sort them afterwards somehow. Twig is not supposed to contain business logic, it's only for rendering elements and sorting arrays is a mess. Creating a Plugin is super easy with pluginfactory.io. If you don't want to do it in php, you can wait for someone else to answer  
> **username_0**: Yeah, guess I've gotta wait for someone with a different solution.  

---

