<p>You need to join the Matrix table and the Matrix content table in that case to order by this specific field. <code>LeftJoin</code> will allow you to only join the first block. If you have several different block ids you need to <code>leftJoin</code> only blocks with your block type ID</p>

<pre><code>{% set field = craft.app.getFields().getFieldByHandle('yourFieldHandle') %}
{% set entries = craft
    .entries()
    .section('entries')
    .leftJoin('{{%matrixblocks}} matrixblocks', '[[matrixblocks.ownerId]] = [[elements.id]]')
    .leftJoin(
        field.contentTable . ' myContentTable',
        '[[myContentTable.elementId]] = [[matrixblocks.id]]'
     )
    .orderBy({'myContentTable.fieldprefix_fieldHandle': SORT_ASC})
    .all()
%}

</code></pre>

<p>change <code>myContentTable.fieldprefix_fieldHandle</code> to your exact DB column or fetch it dynamically.</p>

CC BY-SA 4.0
2019-04-16T14:54:52.313Z
2019-04-16T14:54:52.313Z
Robin Schambach
<p>Can I order entries by the content of a field every entry has?</p>

<pre><code>{% for entry in craft.entries.section('entries').order(  *first time field of a matrix block within the entry*  ).all() %}
</code></pre>

<entries><order>
Order entries by content of their fields
# Order entries by content of their fields

(Asked by: username_0 on 2019-04-16)

Can I order entries by the content of a field every entry has?
{% for entry in craft.entries.section('entries').order(  *first time field of a matrix block within the entry*  ).all() %}

**Tags:** entries, order

---

## 1 answers

### Answer by username_1 on 2019-04-16. Score: 1

You need to join the Matrix table and the Matrix content table in that case to order by this specific field. LeftJoin will allow you to only join the first block. If you have several different block ids you need to leftJoin only blocks with your block type ID
{% set field = craft.app.getFields().getFieldByHandle('yourFieldHandle') %}
{% set entries = craft
    .entries()
    .section('entries')
    .leftJoin('{{%matrixblocks}} matrixblocks', '[[matrixblocks.ownerId]] = [[elements.id]]')
    .leftJoin(
        field.contentTable . ' myContentTable',
        '[[myContentTable.elementId]] = [[matrixblocks.id]]'
     )
    .orderBy({'myContentTable.fieldprefix_fieldHandle': SORT_ASC})
    .all()
%}


change myContentTable.fieldprefix_fieldHandle to your exact DB column or fetch it dynamically.


---

