4

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.

3

Try using the replace filter inside SuperSort's micro-template:

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

{% set sortAsList = {
    'ShopType1': "A",
    'ShopType2': "B",
    'ShopType3': "C"
} %}

{% set sortedFoo = foo | supersort('sortAs', '{ shopType | replace(sortAsList) }') %}

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(sortAsList) } — a Craft 'micro-template' — into {{ object.shopType | replace(sortAsList) }}. The plugin parses that template by passing in your array item as the object and uses the output value to sort by.)

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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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