Is there a way to sort nested collections in Webflow, even with custom code, as I am struggling with sorting the articles on my website?

Published on
September 22, 2023

Yes, it is possible to sort nested collections in Webflow using custom code. Here's how you can achieve this:

  1. Create a custom field for sorting: First, you need to create a custom field for sorting in your CMS collection. For example, you can create a new number field called "Sort Order" or "Priority" that will determine the order in which the articles are displayed.

  2. Assign values to the custom field: Go to your CMS collection and assign a unique number to each article that represents its desired position in the sorted list. Make sure the numbers are sequential and do not have any gaps.

  3. Reference the custom field in the collection list: In your collection list element on the webpage, reference the custom field you created (e.g., "Sort Order" or "Priority") when sorting the items. By default, the collection list will sort based on the creation date, but referencing the custom field will override that.

  4. Publish and test: Once you've set up the custom field and referenced it in the collection list, publish your website and test if the articles are now sorted based on the custom field values.

If you have a large number of items in your collection, you might consider automating the sorting process with custom code. Here's an example using JavaScript:

  1. Create a JavaScript file: Create a new JavaScript file (e.g., custom-sort.js) and save it in a folder named "js".

  2. Link the JavaScript file: In the project settings of your Webflow project, go to the Custom Code tab and paste the following line of code in the Footer Code section:

    ```html

    ```

  3. Write the sorting function: In the custom-sort.js file, write a sorting function that will rearrange the items in the collection list based on the custom field values. Here's an example of how it could look:

    ```javascript
    function sortCollection() {
    var collectionList = document.querySelectorAll('.collection-list .collection-item');
    var sortedList = Array.prototype.slice.call(collectionList)
    .sort(function(a, b) {
    var aValue = parseInt(a.getAttribute('data-sort-order'));
    var bValue = parseInt(b.getAttribute('data-sort-order'));
    return aValue - bValue;
    });

    var parentElement = document.querySelector('.collection-list');sortedList.forEach(function(item) {    parentElement.appendChild(item);});

    }

    window.addEventListener('load', sortCollection);
    ```

  4. Apply the custom attribute: In your CMS collection item template, add a custom attribute to the item wrapper element (e.g., a data-sort-order attribute) and assign it the value of the custom field for each item. This attribute will be used to compare and sort the items.

  5. Publish and test: Save and publish your website to see the sorting function in action.

By following these steps, you should be able to sort nested collections in Webflow using either the built-in sorting feature or custom code. Ensure that you test your changes thoroughly to ensure consistent and accurate sorting.

Additional Questions:

  1. How can I add a custom field for sorting in Webflow CMS?
  2. Can I automate the sorting process for nested collections in Webflow?
  3. What are the steps to sort nested collections using JavaScript in Webflow?