<p>You could use Craft's <a href="https://craftcms.com/docs/templating/filters#group" rel="nofollow"><code>group</code></a> filter for this, but you'll lose the ability to
use the <code>paginate</code> tag to paginate your entries by doing so.</p>

<pre><code>{% set entries = craft.entries.section('mySection').order('title') %}
{% set entriesByType = entries|group('type') %}

{% for type, entriesInType in entriesByType %}
    &lt;h3&gt;{{ type }}&lt;/h3&gt;

    {% for entry in entriesInType %}
        &lt;p&gt;{{ entry.title }}&lt;/p&gt;
    {% endfor %}
{% endfor %}
</code></pre>

CC BY-SA 3.0
2016-04-10T16:07:44.420Z
2016-04-10T16:07:44.420Z
carlcs
<p>You could use my <a href="https://github.com/aelvan/Preparse-Field-Craft" rel="nofollow">Preparse Field plugin</a>, which let's you run twig code when an entry is saved, and saves the result of the twig as plain text in the field.</p>

<p>If you just wanted to sort alphabetically by entry type name, you could put something like this in your preparse field settings:</p>

<pre><code>{{ entry.type | lower }}
</code></pre>

<p>And then go:</p>

<pre><code>{% for craft.entries.section('mySection').order('myPreparseField, title')... %}
</code></pre>

<p>Or, you could do more advanced logic if you want some custom order, like:</p>

<pre><code>{{ entry.type=='type1' ? 1 : (entry.type=='type2' ? 2 : 3) }}
</code></pre>

<p>Make sure you get rid of whitespace in the output, either by not adding any to your code (like above), or by using <code>{{ spaceless }}</code> or <code>-</code> on tags.</p>

CC BY-SA 3.0
2016-04-10T17:19:29.723Z
2016-04-10T17:19:29.723Z
André Elvan
<p>Craft doesn't join the entrytypes table when you query for entries. You could order by <code>typeId</code>, but that's probably not what you want.</p>

<p>Fortunatelly there's an <code>elements.buildElementsQuery</code> event, that allows to modify element queries.</p>

<pre><code>craft()-&gt;on('elements.buildElementsQuery', function(Event $event) {
    $query = $event-&gt;params['query'];

    if (strpos($query-&gt;order, 'entrytypes') !== false) {
        $query-&gt;join('entrytypes entrytypes', 'entrytypes.id = entries.typeId');
    }
});
</code></pre>

<p>Add this code to the <code>init()</code> method of your <a href="https://github.com/lindseydiloreto/craft-businesslogic" rel="nofollow">business logic</a> plugin and you can now order by your entry type handles.</p>

<pre><code>{% set entries = craft.entries({
    section: 'mySection',
    order: 'entrytypes.handle desc, title asc',
}) %}
</code></pre>

CC BY-SA 3.0
2016-04-10T15:58:39.793Z
2016-04-10T15:58:39.793Z
carlcs
<p>I currently have multiple sections like so:</p>

<pre><code>{% for craft.entries.section('mySection').type('oneType')... %}
   ... {% endfor %}
{% for craft.entries.section('mySection').type('anotherType')... %}
   ... {% endfor %}
</code></pre>

<p>But I would rather have a single loop like this:</p>

<pre><code>{% for craft.entries.section('mySection').order('type, title')... %}
</code></pre>

<p>However, the <code>type</code> referred to in <code>order()</code> will in the DB query refer to <code>craft_elements.type</code> which in this case always will be <code>Entry</code>.</p>

<p>Is it possible to order by <em>entry type</em>?</p>

<entries><elementcriteriamodel>
How do I order by _entry type_ when looping through entries in a template?
# How do I order by _entry type_ when looping through entries in a template?

(Asked by: username_0 on 2016-04-10)

I currently have multiple sections like so:
{% for craft.entries.section('mySection').type('oneType')... %}
   ... {% endfor %}
{% for craft.entries.section('mySection').type('anotherType')... %}
   ... {% endfor %}

But I would rather have a single loop like this:
{% for craft.entries.section('mySection').order('type, title')... %}

However, the type referred to in order() will in the DB query refer to craft_elements.type which in this case always will be Entry.
Is it possible to order by entry type?

**Tags:** entries, elementcriteriamodel


---

## 3 answers

### Answer by username_1 on 2016-04-10. Score: 2

You could use Craft's group filter for this, but you'll lose the ability to
use the paginate tag to paginate your entries by doing so.
{% set entries = craft.entries.section('mySection').order('title') %}
{% set entriesByType = entries|group('type') %}

{% for type, entriesInType in entriesByType %}
    {{ type }}

    {% for entry in entriesInType %}
        {{ entry.title }}
    {% endfor %}
{% endfor %}


---

### Answer by username_2 on 2016-04-10. Score: 1

You could use my Preparse Field plugin, which let's you run twig code when an entry is saved, and saves the result of the twig as plain text in the field.
If you just wanted to sort alphabetically by entry type name, you could put something like this in your preparse field settings:
{{ entry.type | lower }}

And then go:
{% for craft.entries.section('mySection').order('myPreparseField, title')... %}

Or, you could do more advanced logic if you want some custom order, like:
{{ entry.type=='type1' ? 1 : (entry.type=='type2' ? 2 : 3) }}

Make sure you get rid of whitespace in the output, either by not adding any to your code (like above), or by using {{ spaceless }} or - on tags.


---

### Answer by username_1 on 2016-04-10. Score: 1

Craft doesn't join the entrytypes table when you query for entries. You could order by typeId, but that's probably not what you want.
Fortunatelly there's an elements.buildElementsQuery event, that allows to modify element queries.
craft()->on('elements.buildElementsQuery', function(Event $event) {
    $query = $event->params['query'];

    if (strpos($query->order, 'entrytypes') !== false) {
        $query->join('entrytypes entrytypes', 'entrytypes.id = entries.typeId');
    }
});

Add this code to the init() method of your business logic plugin and you can now order by your entry type handles.
{% set entries = craft.entries({
    section: 'mySection',
    order: 'entrytypes.handle desc, title asc',
}) %}


---

