How to "ignore" null value in a CMS Sort

My page has a collection list of vehicles with prices. For some vehicles, the price is not known. In the CMS, this field is blank and I am setting conditional statements that display “TBD” on the front end if the price field is blank.

The problem is that when sorting by “Least expensive”, CMS Sort treats blank results as 0 and lists them at the top. A user has to scroll through all of the cars that have unknown prices (some presumed very expensive) before they can see the lowest price vehicles.

Is there a way for CMS Sort to ignore results with a blank value? (and un-ignore them if the sorting method changes - like changing from a price-based sort to an alphabetical sort).

Page is Find Your Electric Vehicle | EV.Guide

Thank you!

Pinging @Support-Luis. Any thoughts on this one?

hey @mike! I’m afraid there is no native way of ignoring the content of the sort fields, however, we could write some JS to manipulate these items.

Let me know what would you like to do to these elements and I can help you write some code.

If you’d like to give it a go this is the callback for CMS Sort →

<script>
  window.fsAttributes = window.fsAttributes || [];
  window.fsAttributes.push([
    'cmssort',
    (listInstances) => {
      console.log('cmssort Successfully loaded!');

      const [listInstance] = listInstances;

      listInstance.on('renderitems', (renderedItems) => {
        // Code to manipulate items without a price goes here
      });
    },
  ]);
</script>

Thank you. If I can’t get rid of these results, I’d like to bury them if possible.

Currently the vehicles with a price of “0” are at the top of the sort order when a user chooses “Sort by least expensive”. I’d like to move them to the bottom of the order, after the most expensive vehicles.

However then if a user selects “sort by most expensive”, I’d like to change the order again so that cars with a price of “0” display before the least expensive option.

This way no matter which option the user chooses to sort by, the null set is always buried at the bottom of the sort.

Is something like that possible? Some sort of conditional override of the default behavior when someone selects a “Sort by least expensive”, but then reverting to default in all other instances?

I don’t have any skill in JS, so I’d love your help on the script if simple/possible.

Thank you!

Hey @mike! I’ve been testing some ideas but I’m afraid since CMS Sort’s API has not been developed yet there is not much we can do.

I added some code to remove the elements and append them to the end of the list but this seemed to break the sorting script and threw a bunch of errors :frowning:

We’ll need to think of another solution

Hm. Thank you for trying. Is it possible to bundle a CMS sort order with a CMS filter? If I create a new CMS item for “known price = true/false”, can I apply that filter “in the background” when someone selects a specific sort order?

This could be super hacky, but maybe even a “hidden” trigger for CMS filter that sits atop the sort order dropdown selection that applies the filter in an invisible way. I would need to figure out a way to deselect this filter if the user chooses a different sort order.

Yes! You could set a toggle that is hidden and only clicking on your desired sort order would activate this filter.

If you could add the toggle to your page I can help you with the code needed, once we have it working we can hide this and the list should behave as you want it to!