Guides / Managing results / Refine results / Sorting results

Using replicas with different sorting strategies allows you to provide a way for users to change the sorting on the front end. For example, imagine you have an ecommerce website, and by default, you want to sort search results from cheapest to most expensive. You might want to provide a dropdown 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 the user changes the sorting.

While you could handle it yourself, it’s easier to use one of the InstantSearch libraries and leverage the built-in 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 via 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?