Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-04-22T10:09:14.150Z
Answers.Text: Any idea why this returns "Unexpected "allOccurrences" tag"?
Answers.UserDisplayName: Clive Portman
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-04-22T10:16:07.580Z
Answers.Text: Removed the error using:
  {% set sortedOccurrences = allOccurrences | supersort('sortAs', sortAsTemplate) %}
Answers.UserDisplayName: Clive Portman
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-04-22T10:16:31.223Z
Answers.Text: Wasn't sorting properly, though, but this fixed it:
  {% set sortAsTemplate = '{{ object.startDate | length ? object.startDate|date("U") : object.date|date("U") }}' %}
Answers.UserDisplayName: Clive Portman
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-04-21T15:57:19.517Z
Answers.LastActivityDate: 2016-04-22T10:26:07.873Z
Answers.LastEditDate: 2016-04-22T10:26:07.873Z
Answers.UserDisplayName: carlcs
Body: <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>
  
Tags: <entries><array><order><merge><sort>
Title: Query two sections filtered by different custom fields, then order them
ThreadText: # 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](https://topshelfcraft.com/lagniappe/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") }}' %}  
  
  ---
  
  