3

Is there a way with SuperSort to sort an array by multiple variables? For example, let's I have the following data and I want to sort by count and then alphabetically:

pineapples (2)
grapefruits (4)
apples (2)
bananas (3)

Ideally, I would want that to be sorted as:

grapefruits (4)
bananas (3)
apples (2)
pineapples (2)

2 Answers 2

4

I was able to accomplish this by just adding both variables to my SuperSort tag. In essence, what I did was this:

{% set fruits = fruits | supersort('natsortas', '{count} {title}') %}

It seems to be working in my situation, but if there's a better way to do it, I'm open to suggestions.

2
  • I changed the sort method to "natsort", to make this work with count numbers greater than 9.
    – carlcs
    Oct 22, 2015 at 17:25
  • There's one problem with this approach. The supersort "as" methods generate a string form the 2nd parameter and use that to sort by. So it basically compares "2 pineapples", "4 grapefruits", "2 apples",... Because of that you can't have different sort orders for each property, so it's either both ascending or not.
    – carlcs
    Oct 22, 2015 at 17:32
1

I just gave it a play and found a simple solution to get them ordered by count in descending order and them by title in ascending order:

{% set fruits = fruits|supersort('natsortas', '{{ (9999-object.count) ~ object.title }}') %}

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.