Sorting categories by the post date of the entries related to them 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.
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') %}
    &lt;li&gt;{{ category }}
        {% for sermon in catSermons %}
            &lt;li&gt;{{ sermon.title }}&lt;/li&gt;
        {% endfor %}
    &lt;/li&gt;
{% endfor %}


Let me know if that works!