<p>There's this super useful <a href="https://topshelfcraft.com/lagniappe/supersort" rel="nofollow">SuperSort</a> plugin that would make this possible.</p>

<pre><code>{% set sortAsTemplate = '{{ object.section == "Exhibitions" ? object.startDate|date("U") : object.date|date("U") }}' %}
{% set sortedOccurrences = allOccurrences|supersort('sortAs', sortAsTemplate) %}
</code></pre>

CC BY-SA 3.0
2016-04-22T10:09:14.150Z
Any idea why this returns "Unexpected "allOccurrences" tag"?
Clive Portman
CC BY-SA 3.0
2016-04-22T10:16:07.580Z
Removed the error using:
{% set sortedOccurrences = allOccurrences | supersort('sortAs', sortAsTemplate) %}
Clive Portman
CC BY-SA 3.0
2016-04-22T10:16:31.223Z
Wasn't sorting properly, though, but this fixed it:
{% set sortAsTemplate = '{{ object.startDate | length ? object.startDate|date("U") : object.date|date("U") }}' %}
Clive Portman
CC BY-SA 3.0
2016-04-21T15:57:19.517Z
2016-04-22T10:26:07.873Z
2016-04-22T10:26:07.873Z
carlcs
<p>Two channels:
1) exhibitions, which have a startDate and EndDate
2) events, which have a date</p>

<p>I need to grab the current exhibitions, future exhibitions and future events, which I do using this:</p>

<pre><code>{% set allOccurrences = craft.entries.section('exhibitions').startDate('&lt;= ' ~ now | date("U")).endDate('&gt;= ' ~ now | date("U"))
    | merge(craft.entries.section('exhibitions').startDate('&gt; ' ~ now | date("U")))
    | merge(craft.entries.section('events').date('&gt;= ' ~ now | date("U"))) %}
</code></pre>

<p>Now what I need to do is order them, by the startDate (if an exhibition) and the date (if an event). In other words, having already fetched the entries, I need to order them by two different fields.</p>

<p>I'm not sure this is possible without a plugin - could anyone give me any pointers?</p>

<entries><array><order><merge><sort>
Query two sections filtered by different custom fields, then order them
# Query two sections filtered by different custom fields, then order them

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

Two channels:
1) exhibitions, which have a startDate and EndDate
2) events, which have a date
I need to grab the current exhibitions, future exhibitions and future events, which I do using this:
{% set allOccurrences = craft.entries.section('exhibitions').startDate('<= ' ~ now | date("U")).endDate('>= ' ~ now | date("U"))
    | merge(craft.entries.section('exhibitions').startDate('> ' ~ now | date("U")))
    | merge(craft.entries.section('events').date('>= ' ~ now | date("U"))) %}

Now what I need to do is order them, by the startDate (if an exhibition) and the date (if an event). In other words, having already fetched the entries, I need to order them by two different fields.
I'm not sure this is possible without a plugin - could anyone give me any pointers?

**Tags:** entries, array, order, merge, sort

---

## 1 answers

### Answer by username_1 on 2016-04-21. Score: 3 | Accepted answer

There's this super useful SuperSort plugin that would make this possible.
{% set sortAsTemplate = '{{ object.section == "Exhibitions" ? object.startDate|date("U") : object.date|date("U") }}' %}
{% set sortedOccurrences = allOccurrences|supersort('sortAs', sortAsTemplate) %}

> **Comments:**
> **username_0**: Any idea why this returns "Unexpected "allOccurrences" tag"?  
> **username_0**: Removed the error using: {% set sortedOccurrences = allOccurrences | supersort('sortAs', sortAsTemplate) %}  
> **username_0**: Wasn't sorting properly, though, but this fixed it: {% set sortAsTemplate = '{{ object.startDate | length ? object.startDate|date("U") : object.date|date("U") }}' %}  

---

