I want to sort an array of entries in a specific order. Every entry has a 'shoptype' and I want to show all entries for type A, than type B, than type C.

A partial solution would be to iterate over them (if shoptype == A){ then show all A entries} but than I can't write a conditonal around it to hide the title when there are no shops of type A.

So now I group them using the group twig filter and hide if the group has no items. But I can't sort them the way I like (it's not alphabetical, it's a fixed order that I want).

Idealy I would need something like this in supersort:

'SortAs', ['shoptype A', 'shoptype B', 'shoptype C']

So an array of strings that my shopType field can contain which my array is grouped by.


2 Answers 2


Edit: This strategy still works, but see my updated answer for more up-to-date solution.

Prior to SuperSort 3.1.0, you might achieve this by using the replace filter inside SuperSort's micro-template:

{% set foo = craft.entries.section('foo')... %}

{% set sortedFoo = foo | supersort('sortAs', '{ shopType | replace({
    'ShopType1': "A",
    'ShopType2': "B",
    'ShopType3': "C"
}) }') %}

The first parameter you pass to SuperSort specifies the sort method.

The second parameter provides a value template for the sorting function: Under the hood, the system parses { shopType | replace(...) } — a Craft 'micro-template' — into {{ object.shopType | replace(...) }}. The plugin parses that template by passing in your array item as the object and uses the output value to sort by.)

n.b. You need to specify that replacement list directly in the object template. Passing in a Twig variable doesn't work because the object template doesn't share the same context as the parent template.

The syntax in your original question would actually not be a bad addition for use-cases like this; I'll look into adding something like that to SuperSort when I get some downtime.

As of SuperSort 3.1.0, you can now use a custom-order sort method, similar to what you originally suggested.

  • I wanted to implement this, but this doesn’t seem to work.. has the syntax changed since 2015?
    – M Smith
    Jan 12, 2021 at 20:59

As of SuperSort 3.1.0, you can use the customOrderSort and customOrderSortAs methods to accomplish this:

{% set shops = craft.entries.section('shops')... %}

{% set customOrderList = [
] %}

{% set sortedShops = shops
    | supersort('customOrderSortAs', as='{shopType}', comp=customOrderList) %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.