<pre><code>-&gt;orderBy('fieldHandle')
</code></pre>

<p>or</p>

<pre><code>-&gt;orderBy(['fieldHandle' =&gt; SORT_DESC])
</code></pre>

<p>But you have to make sure the field is sortable, otherwise it won't work</p>

CC BY-SA 4.0
2018-05-15T16:21:31.777Z
Thanks for the reply! When you say "sortable" could you elaborate? For example, could I sort on a date time field? 

I've tried this exact code and I can get an error saying the field handle doesn't exist and in the query it doesn't look like craft is joining on the actual content table.
Peter Tell
CC BY-SA 4.0
2018-05-15T16:39:20.970Z
Craft won't join the content table in MatrixQueries since Matrix blocks have their custom content tables. You'll have to use the exact column name `$columnName = Craft::$app->getContent()->contentTable . '.' . Craft::$app->getContent()->fieldColumnPrefix . $field->handle;` Only textual fields are sortable.. You can't sort relation fields or something like these
Robin Schambach
CC BY-SA 4.0
2018-05-15T17:19:18.450Z
Oh, this looks like it's for an entry. What about a Matrix?
Peter Tell
CC BY-SA 4.0
2018-05-15T17:26:53.417Z
If I put `->orderBy('craft_matrixcontent_mymatrixfield.field_blockType_fieldName')` I get a DB exception
Peter Tell
CC BY-SA 4.0
2018-05-16T19:15:57.970Z
Sry for the late reply. Did you specify a valid `fieldId` in your query? Otherwise it won't join the correct table and you'll have to do it on your own (I throught that was obvious sorry)
Robin Schambach
CC BY-SA 4.0
2018-05-17T21:29:40.467Z
Not obvious to a dolt like me ;-) Thanks that was it!
Peter Tell
CC BY-SA 4.0
2018-05-18T06:57:21.563Z
When you specify the field id, craft will select the fields with `as fieldHandle` so you don't need to specify the correct database column name. That is only important when you want to join the table on your own. Sry again^^ glad you got it working
Robin Schambach
CC BY-SA 4.0
2018-05-15T15:04:22.453Z
2018-05-15T15:04:22.453Z
Robin Schambach
<p>My goal is to order an element query of Matrix blocks by a custom field. Any thoughts?</p>

<pre><code>namespace modules\mymodule\services;

use Craft;
use yii\base\Component;
use craft\elements\MatrixBlock;

class MyService extends Component
{
    public function myFunction()
    {
        $blocks = MatrixBlock::find()
            -&gt;type('myBlockType')
            -&gt;orderBy(???)
            -&gt;all();
    }
}
</code></pre>

<p><strong>Edit to show working Example</strong></p>

<p>(This is showing how I used this to sort by a custom date field)</p>



<pre><code>$blocks = MatrixBlock::find()
    -&gt;type('blockType')
    -&gt;fieldId($fieldId)
    -&gt;where(['&gt;', 'content.field_blockType_fieldHandle', new \yii\db\Expression('NOW()')])
    -&gt;orderBy(['content.field_blockType_fieldHandle' =&gt; SORT_ASC])
    -&gt;all();
</code></pre>

<plugin-development><matrix><craft3>
Craft 3: When querying for Matrix blocks in a plugin, how do you sort by a custom field?
# Craft 3: When querying for Matrix blocks in a plugin, how do you sort by a custom field?

(Asked by: username_0 on 2018-05-15)

My goal is to order an element query of Matrix blocks by a custom field. Any thoughts?
namespace modules\mymodule\services;

use Craft;
use yii\base\Component;
use craft\elements\MatrixBlock;

class MyService extends Component
{
    public function myFunction()
    {
        $blocks = MatrixBlock::find()
            ->type('myBlockType')
            ->orderBy(???)
            ->all();
    }
}

Edit to show working Example
(This is showing how I used this to sort by a custom date field)
$blocks = MatrixBlock::find()
    ->type('blockType')
    ->fieldId($fieldId)
    ->where(['>', 'content.field_blockType_fieldHandle', new \yii\db\Expression('NOW()')])
    ->orderBy(['content.field_blockType_fieldHandle' => SORT_ASC])
    ->all();

**Tags:** plugin-development, matrix, craft3


---

## 1 answers

### Answer by username_1 on 2018-05-15. Score: 3 | Accepted answer

->orderBy('fieldHandle')

or
->orderBy(['fieldHandle' => SORT_DESC])

But you have to make sure the field is sortable, otherwise it won't work

> **Comments:**
> **username_0**: Thanks for the reply! When you say "sortable" could you elaborate? For example, could I sort on a date time field?   I've tried this exact code and I can get an error saying the field handle doesn't exist and in the query it doesn't look like craft is joining on the actual content table.  
> **username_1**: Craft won't join the content table in MatrixQueries since Matrix blocks have their custom content tables. You'll have to use the exact column name `$columnName = Craft::$app->getContent()->contentTable . '.' . Craft::$app->getContent()->fieldColumnPrefix . $field->handle;` Only textual fields are sortable.. You can't sort relation fields or something like these  
> **username_0**: Oh, this looks like it's for an entry. What about a Matrix?  
> **username_0**: If I put `->orderBy('craft_matrixcontent_mymatrixfield.field_blockType_fieldName')` I get a DB exception  
> **username_1**: Sry for the late reply. Did you specify a valid `fieldId` in your query? Otherwise it won't join the correct table and you'll have to do it on your own (I throught that was obvious sorry)  
> **username_0**: Not obvious to a dolt like me ;-) Thanks that was it!  
> **username_1**: When you specify the field id, craft will select the fields with `as fieldHandle` so you don't need to specify the correct database column name. That is only important when you want to join the table on your own. Sry again^^ glad you got it working  

---

