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' %}
<ul>
{% for s in sorted %}
<li>{{s.name}}</li>
{% endfor %}
</ul>
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' %}
<ul>
{% for s in sorted %}
<li>{{s.name}}</li>
{% endfor %}
</ul>
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.
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.
@Peter-Schmidt please add this suggestion to the public backlog.
Hi guys!
Sorry for my late reply, currently on holiday
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