[Bug] If module items have weighting then sorting by other fields doesn't work

This came up recently in conversation with @Adam.Wilson So maybe he can help with the clarification here. But I find generally if web app items have weighting field filled out then no other sorting methods work. Essentially sort by weight is always applied.

Yes, I’ve found the same. It seems ‘weighting’ overrides everything.
I’m not sure if this is by design or a bug - would need further feedback from the team on this.
It seems like a bug to me though. Certainly let weighting override the default sorting method, but ONLY when no other sorting method is explicitly set.

One possible work-around (and actually a neat way to sort by 2 properties at the same time in some cases) is to use the Liquid sort filter on the collection.

So let’s say I have a Custom Module with 3 items named ‘AAA’, ‘BBB’, and ‘CCC’.
But ‘BBB’ has a weighting set of ‘100’.

The default list will be:

  • BBB
  • AAA
  • CCC

(‘BBB’ gets priority weighting while the rest are sorting alphabetically)

To override this we can do the following:

{% component source: "Sort Test", layout: "", collectionVariable: "sorting", type: "module" %}
{% assign sorted = sorting.items | sort: 'name' %}
{% for s in sorted %}
{% endfor %}

So rending the collection manually and re-sorting the list with Liquid by the ‘Name’ property.
Using this method you can sort by any other property in the collection, eg:

{% assign sorted = sorting.items | sort: 'myCustomProperty' %}

(you can still use list layouts with this method too, by setting the component to use object: "collection" and placing the above code in your list layout instead)

BONUS: To sort by 2 properties at once; let’s say you have a list of people and generally you want to list them by their ‘Company’ names (alphabetically). But some people have an additional ‘Status’ type and you want to list those people first in the list - but still alphabetically by their company names.
We could do something like this:

{% component source: "People", layout: "", sortBy: "Company", sortOrder: "ASC", collectionVariable: "sorting", type: "module" %}
{% assign sorted = sorting.items | sort: 'Status' %}
{% for s in sorted %}
{% endfor %}

TIP: To reverse the Liquid sorting use an additional reverse filter:

{% assign sorted = sorting.items | sort: 'Status' | reverse %}

Hello! Sorting by the field “weight” has priority, then selected fields are sorted. That’s how it works at Treepl.

Honestly @Adam.Wilson you’re a level above. (my *you seeing this … * face) Thank you. That’s so useful.

@Denis.l My suggestion is that if the module insertion specifically requests a sorting method other than weighting then the system should provide that and not just override the requested sorting method. I think it makes sense that if no sorting method is requested via the module insertion and weighting is specified then weighting should be come the default.

1 Like

Hello @Alex_B_Centrifuge. You can add it to the public backlog for review.

@Denis.l That’s why I posted it here. To get it added to the public backlog. :+1:t2:

@Peter-Schmidt please add this suggestion to the public backlog.

1 Like

Hi guys! :slight_smile:

Sorry for my late reply, currently on holiday :beer:
I have added it here: https://treepl.co/public-backlog-state/sorting-by-weighting-overrides-everything

Let me know if it will another title or something added to the description :slight_smile:

1 Like