Instantsearchjs v2 natural sort facets

I have an issue where I am displaying a list of values in a hierarchical menu. The values are for example:

5ft
5ft 1 Inch
5ft 2 Inches
5ft 10 Inches

However, the sort order in incorrect as I am getting:

5ft
5ft 1 Inch
5ft 10 Inches
5ft 2 Inches

I have this existing sort function:

sortBy: function(a, b) {
return parseInt(a.name, 10) - parseInt(b.name, 10);
},

However, this is not sorting naturally. Is there a way with InstantSearch that I can get the order of these values displaying in the correct order, in a natural way ?

Any help would be much appreciated - thanks in advance.

Guy

Hi @guy, from your code it seems to be normal that your sort code is not working, I tried this in my browser console:

parseInt('5ft 10 Inches', 10)
5

While you may expect 10, to solve this you will have to write a regular expression that should isolate the number 10 (or consider 0 if none found) and then you can update your sortBy function to do that.

Good luck, let us know how it goes. If you are struggling with regular expressions, ask on StackOverflow.

Hi @vvo thanks for your response. I’ve posted a question on SO https://stackoverflow.com/questions/51173195/regular-expressions

But the guy that responded said that regular expressions where not best suited for my scenario ???

I’m a bit stuck as to how to implement this correctly. Have you got any other ideas ?

Thanks

Guy