question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Float values in refinement list don't sort properly.

See original GitHub issue

Describe the bug 🐛 Float values in refinement list don’t sort properly.

To Reproduce 🔍 Steps to reproduce the behavior:

  1. Pick a test index
  2. Add size attribute and put values like 9, 9.5, 10, 10.5, 11…
  3. Add size as a facet filter
  4. Add refinement list to your page:
search.addWidget(
  instantsearch.widgets.refinementList({
    container: '#brand-list',
    attributeName: 'size',
    sortBy: ['size:asc'],
  })
);

https://codesandbox.io/s/420yp9lyn0

Expected behavior 💭 Should be sorted 9, 9.5, 10. It’s 9, 10, 9.5 instead.

Screenshots 🖥 image

Environment:

  • OS: Mac
  • Browser: Chrome
  • Version 68.0.3440.106

Additional context Please ping me back when it’s solved so I can contact the customer who reported it.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
bobylitocommented, Sep 10, 2018

Thanks @noclat that’s indeed an annoying problem and definetely unexpected. We should be able to do better.

Looking at a response from Algolia, we can notice two things:

  • the facet values are strings whatever the number they are in the engine
  • if the values are numbers, the facet stats will give us some statistics which is a strong signal about the type.
{
  "results": [
    {
      "hits": [...],
      "nbHits": 15297,
      "page": 0,
      "nbPages": 50,
      "hitsPerPage": 20,
      "processingTimeMS": 11,
      "facets": {
        "price": {
          "39.99": 731,
          "49.99": 699,
          "19.99": 680,
          "29.99": 624,
          "99.99": 590,
          "59.99": 454,
          "24.99": 404,
          "34.99": 354,
          "199.99": 342,
          "79.99": 342
        }
      },
      "facets_stats": {
        "price": {
          "min": 1,
          "max": 4999.99,
          "avg": 258.651,
          "sum": 3956580
        }
      },
      "exhaustiveFacetsCount": true,
      "exhaustiveNbHits": true,
      "query": "h",
      "params": "query=h&maxValuesPerFacet=10&page=0&facets=%5B%22price%22%5D&tagFilters=",
      "index": "instant_search"
    }
  ]
}

We should be able to leverage that information to infer the type of the facet and apply a correct sort but it implies a lot of magic behind the scene.

On the other hand, the user knows better and could easily implement a custom sort function:

search.addWidget(
  instantsearch.widgets.refinementList({
    container: '#brand-list',
    attributeName: 'price',
    sortBy: function(a, b) {
      return Number.parseFloat(a.name) - Number.parseFloat(b.name);
    },
  })
);
0reactions
Haroenvcommented, Aug 23, 2021

This can be solved by using sortBy with a parseFloat (otherwise it sorts alphabetically). As using numerical values for facets is not the main use case (because it usually will explode quickly in number of values, making a range more interesting), I’ll close this issue for now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python how to sort list with float values - Stack Overflow
Hi, just use the sort() methode : >>> list1.sort() · Those are two very different lists. You probably don't really want the second...
Read more >
InstantSearch.js FAQ - Algolia
1. How to use float values in a rating menu widget? ... 7. Why is my facet value disappearing from a refinement list...
Read more >
Filtering and sorting search results - Google Developers
For the Restrict to Range operator, Google supports numbers in float format and dates in ISO 8601 YYYYMMDD without dashes. You do not...
Read more >
[pitch] Comparison Reform - Pitches - Swift Forums
correctly. I don't understand ... No, they are effectively refinement siblings. ... Do you want to be able to sort floating point numbers...
Read more >
Manage the search schema in SharePoint - Microsoft Learn
If the casing is incorrect, no value will be returned. Refine on managed properties. If you want to use a managed property as...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found