Guides / Managing results / Refine results / Sorting results

Using replicas with different sorting strategies provides a way for users to change sorting on the frontend. For example, if you have an ecommerce website, and by default, want to sort search results from cheapest to most expensive. You might want to provide a drop-down menu or toggle switch to let users sort from most expensive to cheapest.

Because you have one replica index per sorting strategy, you need to change what index Algolia must search into when users change the sorting method.

While you could handle it yourself, it’s easier to use the InstantSearch sortBy and relevantSort widgets.

Setting up replicas

Before you implement the search, you need to have replicas for each sorting strategy you want to provide.

Sorting widget

Sort dropdown with sortBy

Dropdowns are a familiar widget, common in ecommerce scenarios and power user workflows. InstantSearch provides sorting dropdowns with the sortBy widget.

1
2
3
4
5
6
7
8
9
search.addWidgets([
  instantsearch.widgets.sortBy({
    container: '#sort-by-container',
    items: [
      { value: 'products', label: 'Most relevant' },
      { value: 'products_price_desc', label: 'Highest price' }
    ]
  })
]);

Relevance sort toggle with relevantSort

If you’re using Relevant sort you can use the relevantSort widget to let users toggle between relevant and regular sorting. The relevantSort widget comes with recommended patterns when using Relevant sort, such as displaying the number of sorted results out of the total number of results, and letting users see more results.

1
2
3
4
5
6
7
8
9
10
search.addWidgets([
  instantsearch.widgets.relevantSort({
    container: '#relevant-sort',
    templates: {
      button({ isRelevantSorted }) {
        return isRelevantSorted ? 'See all results' : 'See relevant results';
      },
    },
  }),
]);
Did you find this page helpful?