<p>One solution is to retrieve the entry ids for the season entries and all entries separately; subtract the 'season entry ids' from 'all entry ids' (using craft's <code>without</code> twig filter); then merge the two arrays back together (using twig's <code>merge</code> filter) in the proper order. You can then use this new id array to generate an elementCriteriaModel which can be used in your <code>paginate</code> expression.</p>

<pre><code>{% cache %}
    {% set season = season.season.first() %}
    {% set seasonEntryIds = craft.entries.section('generalContent').relatedTo(season).order('lft desc').ids() %}
    {% set allEntryIds = craft.entries.section('generalContent').order('lft desc').ids() %}
    {% set nonSeasonEntryIds = allEntryIds|without(seasonEntryIds) %}
    {% set sortedEntryIds = seasonEntryIds|merge(nonSeasonEntryIds) %}

    {% paginate craft.entries.id(sortedEntryIds).fixedOrder(true) as entries %}
        {% for entry in entries %}
            {{ entry.title }}&lt;br&gt;
        {% endfor %}
    {% endpaginate %}
{% endcache %}
</code></pre>

<p>Since there are a lot of queries happening here, it might also be a good idea to wrap the code in 'cache' tag.</p>

CC BY-SA 3.0
2015-06-09T21:12:42.393Z
Douglas, everything looks great up until the sortedEntryIds. For whatever reason, it's putting them back in order in Structure's native Ascending order.
John Johnson
CC BY-SA 3.0
2015-06-09T21:47:23.487Z
Also, not sure if this is a hit to performance, but `nonSeasonEntryIds` doesn't appear to be needed. The merge filter already overrides existing keys in the array. I get the same result with this: `sortedEntryIds = seasonEntryIds|merge(allEntryIds)`
John Johnson
CC BY-SA 3.0
2015-06-10T14:38:33.210Z
Patrick Harrington (mildlygeeky on Slack) gave me the heads up that the parameter `.fixedOrder(true)` should be used on the `{% paginate %}` element. Douglas, if you want to put that in your answer, I'll mark it as answered.
John Johnson
CC BY-SA 3.0
2015-06-10T16:16:25.050Z
Using `merge` and `without` does not create any additional database queries on their own, so shouldn't be any hit to performance either way. I am surprised, however, to hear that the merge filter is overriding existing array items, as we are simply dealing with an array of numbers – and there are many reasons why you might want duplicate numbers in your array. I presume this is because, as you say, there is an underlying key there somewhere  — I'll watch out for that. Regarding `fixedOrder(true)` — great catch —I updated the answer. Regardless — I'm glad you got it working.
Douglas McDonald
CC BY-SA 3.0
2015-06-05T16:21:45.313Z
2015-06-10T16:02:23.833Z
2015-06-10T16:02:23.833Z
Douglas McDonald
<p>I'm picking a season category (spring, summer, fall, winter)  in a global variable that should dictate what entries come to the top of non-season category pages. These entries are already on a category page, sorted by their main category. I need extra sorting to showcase the "Spring" entries, for instance. Something like this:</p>

<p><strong>Without the season global variable chosen</strong></p>

<ol>
<li>Test post 1 (Summer)</li>
<li>Test post 2 (Spring)</li>
<li>Test post 3 (Spring)</li>
<li>Test post 4 (Winter)</li>
<li>Test post 5 (Winter)</li>
</ol>

<p><strong>With the global variable "Spring" chosen</strong></p>

<ol>
<li>Test post 2 (Spring)</li>
<li>Test post 3 (Spring)</li>
<li>Test post 1 (Summer)</li>
<li>Test post 4 (Winter)</li>
<li>Test post 5 (Winter)</li>
</ol>

<p>Basically, I want to bring the entries that are using the chosen Global Variable Category to the top, the remaining entries can be sorted by postDate descending. These entries are in a Structure btw, if that matters.</p>

<p>Structure = 'generalContent'<br>
Category Field = 'season'<br>
Global Variable = 'season.season'</p>

<p>Simplified code of my category page.</p>

<pre><code>{% paginate craft.entries.section('generalContent').relatedTo(category).order('lft desc') as entries %}
  {% for entry in entries %}
    {{ entry.title }}&lt;br&gt;
  {% endfor %}
{% endpaginate %}
</code></pre>

<structure><globals><sorting><plugin-supersort>
Sort entries by global variable category
# Sort entries by global variable category

(Asked by: username_0 on 2015-06-05)

I'm picking a season category (spring, summer, fall, winter)  in a global variable that should dictate what entries come to the top of non-season category pages. These entries are already on a category page, sorted by their main category. I need extra sorting to showcase the "Spring" entries, for instance. Something like this:
Without the season global variable chosen

Test post 1 (Summer)
Test post 2 (Spring)
Test post 3 (Spring)
Test post 4 (Winter)
Test post 5 (Winter)

With the global variable "Spring" chosen

Test post 2 (Spring)
Test post 3 (Spring)
Test post 1 (Summer)
Test post 4 (Winter)
Test post 5 (Winter)

Basically, I want to bring the entries that are using the chosen Global Variable Category to the top, the remaining entries can be sorted by postDate descending. These entries are in a Structure btw, if that matters.
Structure = 'generalContent'
Category Field = 'season'
Global Variable = 'season.season'
Simplified code of my category page.
{% paginate craft.entries.section('generalContent').relatedTo(category).order('lft desc') as entries %}
  {% for entry in entries %}
    {{ entry.title }}
  {% endfor %}
{% endpaginate %}

**Tags:** structure, globals, sorting, plugin-supersort

---

## 1 answers

### Answer by username_1 on 2015-06-05. Score: 4 | Accepted answer

One solution is to retrieve the entry ids for the season entries and all entries separately; subtract the 'season entry ids' from 'all entry ids' (using craft's without twig filter); then merge the two arrays back together (using twig's merge filter) in the proper order. You can then use this new id array to generate an elementCriteriaModel which can be used in your paginate expression.
{% cache %}
    {% set season = season.season.first() %}
    {% set seasonEntryIds = craft.entries.section('generalContent').relatedTo(season).order('lft desc').ids() %}
    {% set allEntryIds = craft.entries.section('generalContent').order('lft desc').ids() %}
    {% set nonSeasonEntryIds = allEntryIds|without(seasonEntryIds) %}
    {% set sortedEntryIds = seasonEntryIds|merge(nonSeasonEntryIds) %}

    {% paginate craft.entries.id(sortedEntryIds).fixedOrder(true) as entries %}
        {% for entry in entries %}
            {{ entry.title }}
        {% endfor %}
    {% endpaginate %}
{% endcache %}

Since there are a lot of queries happening here, it might also be a good idea to wrap the code in 'cache' tag.

> **Comments:**
> **username_0**: Douglas, everything looks great up until the sortedEntryIds. For whatever reason, it's putting them back in order in Structure's native Ascending order.  
> **username_0**: Also, not sure if this is a hit to performance, but `nonSeasonEntryIds` doesn't appear to be needed. The merge filter already overrides existing keys in the array. I get the same result with this: `sortedEntryIds = seasonEntryIds|merge(allEntryIds)`  
> **username_0**: Patrick Harrington (mildlygeeky on Slack) gave me the heads up that the parameter `.fixedOrder(true)` should be used on the `{% paginate %}` element. Douglas, if you want to put that in your answer, I'll mark it as answered.  
> **username_1**: Using `merge` and `without` does not create any additional database queries on their own, so shouldn't be any hit to performance either way. I am surprised, however, to hear that the merge filter is overriding existing array items, as we are simply dealing with an array of numbers – and there are many reasons why you might want duplicate numbers in your array. I presume this is because, as you say, there is an underlying key there somewhere  — I'll watch out for that. Regarding `fixedOrder(true)` — great catch —I updated the answer. Regardless — I'm glad you got it working.  

---

