<p>My <a href="https://github.com/TopShelfCraft/SuperSort" rel="nofollow noreferrer">SuperSort</a> plugin is built for use cases exactly such as this. It allows you to render each object in your array using a Twig object template and sort the array based on the rendered value.</p>

CC BY-SA 3.0
2015-01-24T00:36:43.580Z
2017-08-18T14:53:53.950Z
2017-08-18T14:53:53.950Z
Michael Rog
<p>You may be able to do the sorting with a bit of manual work. What you would need to do is to convert your <code>ECM</code> to a <code>DBCommand</code> and then manually join the matrix-field table that corresponds to the field you want to order by.</p>

<p>See <a href="https://craftcms.stackexchange.com/questions/4600/order-two-entry-types-by-different-date-fields-for-use-in-one-combined-stream">Order two entry-types by different date fields for use in one combined stream</a> for the approach I have taken to combine two entry types in a query, where one of them gets ordered by its matrix-field.</p>

CC BY-SA 3.0
2014-11-12T10:03:21.347Z
2014-11-12T10:03:21.347Z
Clemens Sum
<p>Used super <a href="https://github.com/TopShelfCraft/SuperSort" rel="nofollow noreferrer">sort plugin</a> and got away with this:</p>

<pre><code>  {% for item in entries|supersort('sortAs', '{dateSet.first.startDate|date("c")}') %}
</code></pre>

CC BY-SA 4.0
2020-03-31T15:20:24.750Z
2020-03-31T15:20:24.750Z
medoingthings
<p>Your example is referencing a field that Twig doesn't know about yet. Also, you usually place the fields you are ordering entries with in quotes (ex: <code>order('dateField')</code>).</p>

<p>Matrix blocks are elements (like so many things in Craft), and thus are unable to be used for ordering entries. THe reason is that the variable in the returned model is not the actual blocks, but a Criteria Model for retrieving those blocks. It's a reference, if you will. I suggest you create a separate date field on the entry for ordering.</p>

CC BY-SA 3.0
2014-07-17T13:39:20.860Z
2014-07-17T13:39:20.860Z
Bryan Redeagle
<p>I have a section where its entries have a matrix field (<code>matrixField</code>). In the first matrix block there is a date field (<code>dateField</code>). I want to get my entries ordered by that date field. Is this possible? Pseudocode:</p>

<pre><code>{% for entry in craft.entries.section('dates').order(matrixField.first().dateField).limit(5) %}
    ...
</code></pre>

CC BY-SA 3.0
2014-07-08T01:49:44.000Z
It looks like what you have might work as is, how does it fail?
Simon Kuran
CC BY-SA 3.0
2014-07-13T01:07:57.367Z
Did you find a solution to your problem?
Tyler Willingham
CC BY-SA 3.0
2014-07-13T21:37:11.643Z
@yoursort Any luck?
Matt Stein
CC BY-SA 3.0
2014-07-15T15:37:05.047Z
No it fails with `Variable "matrixField" does not exist`. The Craft docs say that "*Possible values include 'title', 'id', ..., as well as any textual custom field handles.*" A matrix field is not a textual field and I do not think that  the expression inside the order parameter gets evaluated.
Lars Bilke
<entries><matrix><datetime><ordering>
How to order by matrix field?
# How to order by matrix field?

(Asked by: username_0 on 2014-07-07)

I have a section where its entries have a matrix field (matrixField). In the first matrix block there is a date field (dateField). I want to get my entries ordered by that date field. Is this possible? Pseudocode:
{% for entry in craft.entries.section('dates').order(matrixField.first().dateField).limit(5) %}
    ...

**Tags:** entries, matrix, datetime, ordering

> **Comments:**
> **username_1**: It looks like what you have might work as is, how does it fail?  
> **username_2**: Did you find a solution to your problem?  
> **username_3**: @yoursort Any luck?  
> **username_0**: No it fails with `Variable "matrixField" does not exist`. The Craft docs say that "*Possible values include 'title', 'id', ..., as well as any textual custom field handles.*" A matrix field is not a textual field and I do not think that  the expression inside the order parameter gets evaluated.  

---

## 4 answers

### Answer by username_7 on 2014-07-17. Score: 5 | Accepted answer

Your example is referencing a field that Twig doesn't know about yet. Also, you usually place the fields you are ordering entries with in quotes (ex: `order('dateField')`).

Matrix blocks are elements (like so many things in Craft), and thus are unable to be used for ordering entries. THe reason is that the variable in the returned model is not the actual blocks, but a Criteria Model for retrieving those blocks. It's a reference, if you will. I suggest you create a separate date field on the entry for ordering.


---

### Answer by username_4 on 2015-01-24. Score: 8

My [SuperSort](https://github.com/TopShelfCraft/SuperSort) plugin is built for use cases exactly such as this. It allows you to render each object in your array using a Twig object template and sort the array based on the rendered value.


---

### Answer by username_5 on 2014-11-12. Score: 2

You may be able to do the sorting with a bit of manual work. What you would need to do is to convert your `ECM` to a `DBCommand` and then manually join the matrix-field table that corresponds to the field you want to order by.

See [Order two entry-types by different date fields for use in one combined stream](https://craftcms.stackexchange.com/questions/4600/order-two-entry-types-by-different-date-fields-for-use-in-one-combined-stream) for the approach I have taken to combine two entry types in a query, where one of them gets ordered by its matrix-field.


---

### Answer by username_6 on 2020-03-31. Score: 1

Used super sort plugin and got away with this:
  {% for item in entries|supersort('sortAs', '{dateSet.first.startDate|date("c")}') %}


---

