How to sort the filterable options?

Hi, I am using Algolia Magento 2 module and currently I am facing an issue that where I need to have a custom sorting for options of a few attributes. This sorting isn’t alphabetical or numeric is just based on a list the customer gave to me.

How would I do this?

Hi!

By default, Algolia sorts by best relevance. You can however define a custom sorting strategy yourself: https://community.algolia.com/magento/doc/m2/indexing/#sorting-strategies

Please note that each sorting strategy will multiply the number of records that will be indexed in Algolia, as we use replica indices to provide different sorting strategies. You can read more on how Algolia handles sorting in our documentation: https://www.algolia.com/doc/guides/ranking/sorting/

Have a nice day!

Thanks for your reply! But isn’t this the sorting the order of attributes and not the sorting of the values of an attribute.

I fixed it by overriding the instantsearch.js in my theme. Then adding a custom sorting function in the window.getFacetWidget method.

if (facet.attribute === "product_materials") {
					sortFunction = function (a, b) {
						let map = {
                            "P": 6, "M": 5, "K": 4,
                            "N": 3, "S": 2, "H": 1
						};
						let av = map[a.name.slice(-1)],
							bv = map[b.name.slice(-1)];
                        if (av < bv) return 1;
                        if (av === bv) return 0;
                        if (av > bv) return  -1;
                        if (bv === undefined) return -1;
                        if (av === undefined) return -1;
					};
                }

It’s not very elegant, especial if you have a few of this kind of attributes but it works.

1 Like