<p>In this case, I'd probably use the <code>group</code> filter as described in other answers.</p>

<p>However, if you had sermons in multiple categories, only grouping them under their <em>first</em> category might not work.</p>

<p>In that case, you could query the categories first, and use my <a href="http://michaelrog.com/craft/lagniappe/supersort" rel="nofollow">SuperSort</a> plugin to sort them based on a value computed by Twig, such as the number of related entries. (SuperSort allows you to render each object in an array using a Twig object template and sort the array based on the rendered value.)</p>

CC BY-SA 3.0
2015-01-24T00:28:53.787Z
2015-01-24T00:28:53.787Z
Michael Rog
<p>Alan,</p>

<p>Without having tested this, you might get the desired result by using the <a href="http://buildwithcraft.com/docs/templating/filters#group" rel="nofollow">group</a> filter.</p>

<p>So, get all you entries ordered by dateCreated, and then group them by category:</p>

<pre><code>{% set sermons = craft.entries.section('sermon').order('dateCreated desc').limit(null) %}
{% for category, catSermons in sermons | group('categoryFieldHandle.first().title') %}
    &lt;li&gt;{{ category }}
        {% for sermon in catSermons %}
            &lt;li&gt;{{ sermon.title }}&lt;/li&gt;
        {% endfor %}
    &lt;/li&gt;
{% endfor %}
</code></pre>

<p>Let me know if that works!</p>

CC BY-SA 3.0
2014-09-19T20:12:39.007Z
That's a great idea! I tried it out and is giving me the following error: Object of class Craft\ElementCriteriaModel could not be converted to string
I was able to return results by using group('categoryFieldHandle.first().title') then I could just get the data related to that category by making another database call. I do wonder if there is a way I can return the whole CategoryModel with this group parameter. Anyone have any ideas?
Alan Miller
CC BY-SA 3.0
2014-09-19T20:18:11.340Z
You can get the CategoryModel from any one of the sermons, you could set it from the first sermon for each category like: {% if categoryModel is not defined or categoryModel.title is not category %} {% set category = sermon.categoryFieldHandle %} {% endif %} or something like that. No extra query required ...
Paul
CC BY-SA 3.0
2014-09-19T07:59:29.793Z
2014-09-25T17:40:20.820Z
2014-09-25T17:40:20.820Z
Paul
<p>I need to sort my categories based on the date of entries returned within the category. I am trying to sort sermons from a church by the series they were contained within. I have the sermons grouped by their series and outputting correctly on that front but they display in order based on the category creation date.</p>

<p>I need the sermon series to show in chronological descending order.</p>

<p>So we have Sermon Series A, Sermon Series B, Sermon Series C... within each one of those categories we have individual sermons related to the category. For example:</p>

<ul>
<li><p>Sermon Series B</p>

<ul>
<li>Sermon 1 (1/10/2013)</li>
<li>Sermon 2 (1/17/2013)</li>
</ul></li>
<li><p>Sermon Series C</p>

<ul>
<li>Sermon 1 (9/10/2014)</li>
<li>Sermon 2 (9/17/2014)</li>
</ul></li>
<li><p>Sermon Series A</p>

<ul>
<li>Sermon 1 (5/10/2014)</li>
<li>Sermon 2 (5/17/2014)</li>
</ul></li>
</ul>

<p>Currently my output looks very similar to the above example but what I need is for it to show like this:</p>

<ul>
<li><strong>Sermon Series C</strong>
<ul>
<li>Sermon 1 (9/10/2014)</li>
<li>Sermon 2 (9/17/2014)</li>
</ul></li>
<li><strong>Sermon Series A</strong>
<ul>
<li>Sermon 1 (5/10/2014)</li>
<li>Sermon 2 (5/17/2014)</li>
</ul></li>
<li><strong>Sermon Series B</strong>
<ul>
<li>Sermon 1 (1/10/2013)</li>
<li>Sermon 2 (1/17/2013)</li>
</ul></li>
</ul>

<p>Since the entries contained within the categories are in that order by post date.</p>

<p>Here is my current Twig code:</p>

<pre><code>{% set entries = craft.entries.section('sermon').limit(null) %}

{% for category in craft.categories.group('sermonSeries').relatedTo(entries).order('dateCreated desc') %}

  {% for entry in entries.relatedTo(category) %}
</code></pre>

<p>But as I stated above this doesn't output my content with the desired effect.</p>

<categories><datetime><ordering>
Sorting categories by the post date of the entries related to them
# Sorting categories by the post date of the entries related to them

(Asked by: username_0 on 2014-09-19)

I need to sort my categories based on the date of entries returned within the category. I am trying to sort sermons from a church by the series they were contained within. I have the sermons grouped by their series and outputting correctly on that front but they display in order based on the category creation date.
I need the sermon series to show in chronological descending order.
So we have Sermon Series A, Sermon Series B, Sermon Series C... within each one of those categories we have individual sermons related to the category. For example:

Sermon Series B

Sermon 1 (1/10/2013)
Sermon 2 (1/17/2013)

Sermon Series C

Sermon 1 (9/10/2014)
Sermon 2 (9/17/2014)

Sermon Series A

Sermon 1 (5/10/2014)
Sermon 2 (5/17/2014)


Currently my output looks very similar to the above example but what I need is for it to show like this:

Sermon Series C

Sermon 1 (9/10/2014)
Sermon 2 (9/17/2014)

Sermon Series A

Sermon 1 (5/10/2014)
Sermon 2 (5/17/2014)

Sermon Series B

Sermon 1 (1/10/2013)
Sermon 2 (1/17/2013)


Since the entries contained within the categories are in that order by post date.
Here is my current Twig code:
{% set entries = craft.entries.section('sermon').limit(null) %}

{% for category in craft.categories.group('sermonSeries').relatedTo(entries).order('dateCreated desc') %}

  {% for entry in entries.relatedTo(category) %}

But as I stated above this doesn't output my content with the desired effect.

**Tags:** categories, datetime, ordering


---

## 2 answers

### Answer by username_2 on 2014-09-19. Score: 5 | Accepted answer

Alan,
Without having tested this, you might get the desired result by using the group filter.
So, get all you entries ordered by dateCreated, and then group them by category:
{% set sermons = craft.entries.section('sermon').order('dateCreated desc').limit(null) %}
{% for category, catSermons in sermons | group('categoryFieldHandle.first().title') %}
    {{ category }}
        {% for sermon in catSermons %}
            {{ sermon.title }}
        {% endfor %}
    
{% endfor %}

Let me know if that works!

> **Comments:**
> **username_0**: That's a great idea! I tried it out and is giving me the following error: Object of class Craft\ElementCriteriaModel could not be converted to string I was able to return results by using group('categoryFieldHandle.first().title') then I could just get the data related to that category by making another database call. I do wonder if there is a way I can return the whole CategoryModel with this group parameter. Anyone have any ideas?  
> **username_2**: You can get the CategoryModel from any one of the sermons, you could set it from the first sermon for each category like: {% if categoryModel is not defined or categoryModel.title is not category %} {% set category = sermon.categoryFieldHandle %} {% endif %} or something like that. No extra query required ...  

---

### Answer by username_1 on 2015-01-24. Score: 2

In this case, I'd probably use the `group` filter as described in other answers.

However, if you had sermons in multiple categories, only grouping them under their *first* category might not work.

In that case, you could query the categories first, and use my [SuperSort](http://michaelrog.com/craft/lagniappe/supersort) plugin to sort them based on a value computed by Twig, such as the number of related entries. (SuperSort allows you to render each object in an array using a Twig object template and sort the array based on the rendered value.)


---

