Default sort when using list combine

Hey @Support-Luis!

Trying to get a default sort working with combine. I used a snippet from another thread, but it’s doing something funky. Here is my setup and overview of the issue:

Sandbox - Webflow - Ambient AI - New UI

Staging URL - Resources

Hey @hello6!

Your HTML structure looks correct with all the right attributes in place for fs-list-combine with filtering and loading functionality :flexed_biceps:

This “funky behavior” with default sorting in combine setups is typically a timing issue where sorting happens before combine and load features fully initialize.

Here’s a callback that will handle default sorting with your combine setup:

<script>
  window.FinsweetAttributes = window.FinsweetAttributes || [];
  window.FinsweetAttributes.push([
    'list',
    (listInstances) => {
      const [listInstance] = listInstances;

      listInstance.addHook('afterRender', (items) => {
        listInstance.sorting.value.fieldKey = 'date';
        listInstance.sorting.value.direction = 'desc'; 
      });
    },
  ]);
</script>

Make sure your date field has fs-list-fieldtype="date" attribute since you’re sorting by date, as seen on the docs. This callback waits for the combine and load processes to complete before applying the sort.

Another option is to use the renderitems event if you need more precise timing control :thinking: But the afterRender hook is usually more reliable for combine + load scenarios.

Add the script to your page’s custom code section (Before tag).

If you need more help with this, @Support-Luis, @Support-Pedro or @jesse.muiruri can assist further.

Worked a treat, thanks @Support-Finn

Hey @Support-Finn, is there a way to sort first by a featured toggle in the collections?

Hi @hello6

Could you expound? You mean sort by a featured toggle automatically on page load?

Also considering that the earlier issue is resolved, please consider making a new topic on this so the whole team is alerted

Hey @jesse.muiruri, the combined collections all have a toggle to mark items as featured.

On pages that aren’t combining the collections, I’m filtering first by featured and then date.

This works great, but I need to do it on the page with the combined collections. If possible I’d like to leave everything in this thread as I’m sure it’s an update to the script from the previous solution that initiates the sorting.

Hey @hello6

I get you now, please try out this code

<script>
window.FinsweetAttributes ||= [];
  window.FinsweetAttributes.push([
    "list",
    (listInstances) => {
      console.log("List Successfully loaded!");

      const [listInstance] = listInstances;

      listInstance.addHook('afterRender', () => {

      listInstance.sorting.value = {
        fields: [
          {
            fieldKey: 'featured',
            direction: 'desc',
          },
          {
            fieldKey: 'date',
            direction: 'desc',
          },
        ],
      };

    });
  },
]);
</script>

Hey @jesse.muiruri, sorry I didn’t get a notification for this, hence the delay. I tried your code, but no luck - Resource Hub | Ambient.ai

Do we need to define a hidden field for “featured”? I can’t see the toggle in the selector for fs-list-value

Hey @jesse.muiruri, any luck?

Hey @hello6, please create a new post for this so the team can get back to you fast enough, thanks :folded_hands:

Done, see - Multiple default sorting when using list combine