<p>The natural sort order for related entries is the order defined in the control panel (not the post date of the related entries), unless you otherwise specify the sort order. Here is an example taken from one of my projects, where related articles are defined within an entries field-type in an article entry:</p>

<pre><code>{% set relatedArticles = entry.relatedArticles %}
&lt;ul&gt;
    {% for article in relatedArticles %}
        &lt;li&gt;&lt;a href="{{ article.url }}"&gt;{{ article.title }}&lt;/a&gt;&lt;/li&gt;
    {% endfor %}
&lt;/ul&gt;
</code></pre>

<p>On the backend, craft uses the table <code>craft_relations</code> to create the relationship with the columns <code>sourceId</code>, <code>targetId</code>, and <code>sortOrder</code>. This <code>sortOrder</code> column defines the default sort order when accessing related entries through the related 'entries' field-type.</p>

<p>If on the other hand, you are accessing related entries using the <code>.relatedTo()</code> method of the EntryCriteriaModel then you will need to specify the sort order using <code>.order('sortOrder')</code>.</p>

<p><strong>Update</strong></p>

<p>Here is the original code:</p>

<pre><code>{% set relatedRecords = craft.entries.section('contenuDiscographie').relatedTo(entry) %} 
{% for record in relatedRecords %} 
    &lt;a href="{{ record.url }}"&gt;{{ record.title }}&lt;/a&gt;    
    {% for block in record.discoArtistes %}    
        {% set relatedPerformers = craft.entries.section('interpretes').relatedTo(block) %}
        {% for performer in relatedPerformers %}     
            {{ performer.title }}
        {% endfor %}    
    {% endfor %} 
{% endfor %}
</code></pre>

<p>Try using the actual relationship field:</p>

<pre><code>{% set relatedRecords = entry.myRelatedEntriesFieldHandle %} 
{% for record in relatedRecords %} 
    &lt;a href="{{ record.url }}"&gt;{{ record.title }}&lt;/a&gt;    
    {% for block in record.myMatrixBlockFieldHandle %}
        {% set relatedPerformers = block.myRelatedEntriesFieldHandleInsideMatrixField %}
        {% for performer in relatedPerformers %}      
            {{ performer.title }}
        {% endfor %}    
    {% endfor %} 
{% endfor %}
</code></pre>

<p><strong>Edit</strong> I edited the code above to be more generic, rather than trying to guess what the field handles are named in your project. You need to change these to your actual field handles. From the picture above I would guess they might be 'entry.relatedRecords', 'record.discoArtistes', and 'block.nom' for example.</p>

CC BY-SA 3.0
2014-11-20T09:33:45.830Z
This doesn't work. `{% set relatedPerformers = block.relatedPerformers %}`returns an error. I tried `= block.MatrixBlockName` but no more success.
jjem
CC BY-SA 3.0
2014-11-20T09:36:09.310Z
the error: Craft\MatrixBlockModel and its behaviors do not have a method or closure named "relatedPerformers".
jjem
CC BY-SA 3.0
2014-11-20T14:29:52.247Z
You need to adapt it to use the field handle of your related entries field. So use `{% set relatedPerformers = block.theFieldHandleOfMyRelatedEntriesField %}`
Douglas McDonald
CC BY-SA 3.0
2014-11-20T14:59:34.327Z
This was done. But it doesn't accept the Matrix field handle (discoArtistes). Also tried we the Matrix field block handle....
jjem
CC BY-SA 3.0
2014-11-20T15:35:07.093Z
There are two fieldHandles we are talking about. There is the 'matrixField handle' and there is the 'entriesField handle' inside the matrixField. What is the name of the entries field handle inside the block?
Douglas McDonald
CC BY-SA 3.0
2014-11-20T15:44:09.493Z
OK, got it! Thanks. This made it: `{% set relatedPerformers = block.nom.order('sortOrder') %}`
jjem
CC BY-SA 3.0
2014-11-20T15:53:25.043Z
Great! Keep in mind that you do not need the `.order('sortOrder')` as this is the default.
Douglas McDonald
CC BY-SA 3.0
2014-11-10T12:28:58.883Z
2014-11-20T15:39:29.380Z
2014-11-20T15:39:29.380Z
Douglas McDonald
<p>Set the <code>order</code> parameter to <code>'sortOrder'</code>:</p>

<pre><code>{% set entries = craft.entries.relatedTo(entry).order('sortOrder') %}
</code></pre>

<p>or do simply use the <a href="http://buildwithcraft.com/docs/entries-fields#templating" rel="nofollow">short syntax</a>:</p>

<pre><code>{% set entries = entry.entriesFieldHandle %}
</code></pre>

CC BY-SA 3.0
2014-11-10T12:37:37.780Z
this gives: SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'sortOrder' in order clause is ambiguous.....
jjem
CC BY-SA 3.0
2014-11-10T12:42:46.493Z
Does this work: `{% set entries = craft.entries.relatedTo({ sourceElement: entry, field: 'entriesFieldHandle' }).order('sortOrder') %}`
carlcs
CC BY-SA 3.0
2014-11-10T12:29:43.290Z
2014-11-10T12:29:43.290Z
carlcs
<p>Try this(sorted by 'title'):</p>
<pre><code>{% for entry in relatedEntries|sort((a, b) =&gt; a.title &lt;=&gt; b.title) %}
    &lt;a href=&quot;{{ entry.url }}&quot;&gt;{{ entry.title }}&lt;/a&gt;&lt;br&gt;
{% endfor %}
</code></pre>
<p>Twig docs: <a href="https://twig.symfony.com/doc/2.x/filters/sort.html" rel="nofollow noreferrer">Sorts an array</a></p>

CC BY-SA 4.0
2022-01-28T03:38:50.267Z
2022-01-28T03:38:50.267Z
Nikolay Bronskiy
<p>After testing the above solution by Douglas</p>

<p>1) If the related entries are not in a Matrix block the following works perfectly. You may put the related entries in any custom order by dragging them around.</p>

<pre><code>{% set relatedPerformers = entry.performer.order('sortOrder') %}
</code></pre>

<p>2) The related entries coming from a Matrix field gives following error:
Craft\MatrixBlockModel and its behaviors do not have a method or closure named "relatedPerformers".</p>

<pre><code>{% set relatedPerformers = block.performer.order('sortOrder') %}
</code></pre>

CC BY-SA 3.0
2014-11-20T15:11:03.257Z
You should generally use comments for this rather than posting it as an answer (or edit your original question to provide more details). You do not need to include the `.order('sortOrder')` when accessing the related entries through an `entries` field. They will be ordered by 'sortOrder' by default.
Douglas McDonald
CC BY-SA 3.0
2014-11-20T12:14:45.103Z
2014-11-20T13:48:33.887Z
2014-11-20T13:48:33.887Z
jjem
<p>is it possible to replicate the order of related entries in the front end as they appear in the admin entries panel? (see images below)</p>

<p>Right now, in the front end it only sorts the order in which the entries where originally entered (sorted by entry date &amp; time).</p>

<p>If not is there any other possibility to do so?
Thanks</p>

<p><img src="https://i.sstatic.net/M9WUM.png" alt="enter image description here"></p>

<p><img src="https://i.sstatic.net/cpB4O.png" alt="enter image description here"></p>

CC BY-SA 3.0
2014-11-10T12:22:31.020Z
What does your template code for outputting this look like? If you loop through the entriy field directly, you should get the order in the entry field.
André Elvan
CC BY-SA 3.0
2014-11-10T12:43:39.530Z
`{% set relatedRecords = craft.entries.section('contenuDiscographie').relatedTo(entry) %}
{% for record in relatedRecords %}
<a href="{{ record.url }}">{{ record.title }}</a>
   {% for block in record.discoArtistes %}
   {% set relatedPerformers = craft.entries.section('interpretes').relatedTo(block) %}
   {% for performer in relatedPerformers %} // this needs to be sorted //
      {{ performer.title }}
   {% endfor %}
   {% endfor %}
{% endfor %}
{% endif %}`
jjem
CC BY-SA 3.0
2014-11-10T13:10:08.207Z
It would be best to edit your original question and add the code example there. It's difficult to read in the comments. I updated my answer to show the original and the modified code.
Douglas McDonald
<sorting>
Custom order related entries
# Custom order related entries

(Asked by: username_0 on 2014-11-10)

is it possible to replicate the order of related entries in the front end as they appear in the admin entries panel? (see images below)

Right now, in the front end it only sorts the order in which the entries where originally entered (sorted by entry date & time).

If not is there any other possibility to do so?
Thanks

![enter image description here](https://i.sstatic.net/M9WUM.png)

![enter image description here](https://i.sstatic.net/cpB4O.png)

**Tags:** sorting

> **Comments:**
> **username_1**: What does your template code for outputting this look like? If you loop through the entriy field directly, you should get the order in the entry field.  
> **username_0**: `{% set relatedRecords = craft.entries.section('contenuDiscographie').relatedTo(entry) %} {% for record in relatedRecords %} <a href="{{ record.url }}">{{ record.title }}</a>    {% for block in record.discoArtistes %}    {% set relatedPerformers = craft.entries.section('interpretes').relatedTo(block) %}    {% for performer in relatedPerformers %} // this needs to be sorted //       {{ performer.title }}    {% endfor %}    {% endfor %} {% endfor %} {% endif %}`  
> **username_2**: It would be best to edit your original question and add the code example there. It's difficult to read in the comments. I updated my answer to show the original and the modified code.  

---

## 4 answers

### Answer by username_2 on 2014-11-10. Score: 4 | Accepted answer

The natural sort order for related entries is the order defined in the control panel (not the post date of the related entries), unless you otherwise specify the sort order. Here is an example taken from one of my projects, where related articles are defined within an entries field-type in an article entry:
{% set relatedArticles = entry.relatedArticles %}

    {% for article in relatedArticles %}
        {{ article.title }}
    {% endfor %}


On the backend, craft uses the table craft_relations to create the relationship with the columns sourceId, targetId, and sortOrder. This sortOrder column defines the default sort order when accessing related entries through the related 'entries' field-type.
If on the other hand, you are accessing related entries using the .relatedTo() method of the EntryCriteriaModel then you will need to specify the sort order using .order('sortOrder').
Update
Here is the original code:
{% set relatedRecords = craft.entries.section('contenuDiscographie').relatedTo(entry) %} 
{% for record in relatedRecords %} 
    {{ record.title }}    
    {% for block in record.discoArtistes %}    
        {% set relatedPerformers = craft.entries.section('interpretes').relatedTo(block) %}
        {% for performer in relatedPerformers %}     
            {{ performer.title }}
        {% endfor %}    
    {% endfor %} 
{% endfor %}

Try using the actual relationship field:
{% set relatedRecords = entry.myRelatedEntriesFieldHandle %} 
{% for record in relatedRecords %} 
    {{ record.title }}    
    {% for block in record.myMatrixBlockFieldHandle %}
        {% set relatedPerformers = block.myRelatedEntriesFieldHandleInsideMatrixField %}
        {% for performer in relatedPerformers %}      
            {{ performer.title }}
        {% endfor %}    
    {% endfor %} 
{% endfor %}

Edit I edited the code above to be more generic, rather than trying to guess what the field handles are named in your project. You need to change these to your actual field handles. From the picture above I would guess they might be 'entry.relatedRecords', 'record.discoArtistes', and 'block.nom' for example.

> **Comments:**
> **username_0**: This doesn't work. `{% set relatedPerformers = block.relatedPerformers %}`returns an error. I tried `= block.MatrixBlockName` but no more success.  
> **username_0**: the error: Craft\MatrixBlockModel and its behaviors do not have a method or closure named "relatedPerformers".  
> **username_2**: You need to adapt it to use the field handle of your related entries field. So use `{% set relatedPerformers = block.theFieldHandleOfMyRelatedEntriesField %}`  
> **username_0**: This was done. But it doesn't accept the Matrix field handle (discoArtistes). Also tried we the Matrix field block handle....  
> **username_2**: There are two fieldHandles we are talking about. There is the 'matrixField handle' and there is the 'entriesField handle' inside the matrixField. What is the name of the entries field handle inside the block?  
> **username_0**: OK, got it! Thanks. This made it: `{% set relatedPerformers = block.nom.order('sortOrder') %}`  
> **username_2**: Great! Keep in mind that you do not need the `.order('sortOrder')` as this is the default.  

---

### Answer by username_3 on 2014-11-10. Score: 2

Set the order parameter to 'sortOrder':
{% set entries = craft.entries.relatedTo(entry).order('sortOrder') %}

or do simply use the short syntax:
{% set entries = entry.entriesFieldHandle %}

> **Comments:**
> **username_0**: this gives: SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'sortOrder' in order clause is ambiguous.....  
> **username_3**: Does this work: `{% set entries = craft.entries.relatedTo({ sourceElement: entry, field: 'entriesFieldHandle' }).order('sortOrder') %}`  

---

### Answer by username_4 on 2022-01-28. Score: 1

Try this(sorted by 'title'):
{% for entry in relatedEntries|sort((a, b) => a.title <=> b.title) %}
    {{ entry.title }}
{% endfor %}

Twig docs: Sorts an array


---

### Answer by username_0 on 2014-11-20. Score: 1

After testing the above solution by Douglas
1) If the related entries are not in a Matrix block the following works perfectly. You may put the related entries in any custom order by dragging them around.
{% set relatedPerformers = entry.performer.order('sortOrder') %}

2) The related entries coming from a Matrix field gives following error:
Craft\MatrixBlockModel and its behaviors do not have a method or closure named "relatedPerformers".
{% set relatedPerformers = block.performer.order('sortOrder') %}

> **Comments:**
> **username_2**: You should generally use comments for this rather than posting it as an answer (or edit your original question to provide more details). You do not need to include the `.order('sortOrder')` when accessing the related entries through an `entries` field. They will be ordered by 'sortOrder' by default.  

---

