With sorting strategies, you can sort at query time based on price, recency, popularity, or custom business logic from your Algolia indices.
Define sorting strategies with the API
To configure sorting behavior programmatically, use the Composition API. The following explains how to set and apply sorting strategies.Set sorting strategies in a composition
You can use the Composition API to configure the sorting strategy programmatically. The sorting strategies are defined in thesortingStrategy parameter (available at root level):
JSON
sortingStrategy is a key/value store where:
- Key is the sorting label your UI will send at run time (for example
Price (asc)) - Value is the Algolia index or replica to use when this sorting is selected (for example
Products-sorted-by-price-low-to-high)
Select a sorting strategy at runtime
When querying your composition, you can select the sorting strategy to apply to Smart Group results by sending thesortBy parameter with your .
JSON
- The value sent with
sortBymust match one of the keys defined in the composition’ssortingStrategymap. If no match is found,sortByis ignored. - The order of items defined in the Smart Group remains fixed. Only the are affected by
sortBy. - If a composition rule applies to the query, it’s applied first. Then, the sorting strategy affects the defined by that rule.
Integrate with InstantSearch
Composition sorting strategies require these versions of the InstantSearch libraries:- React InstantSearch: version 7.22.1 or later
- InstantSearch.js: version 4.86.1 or later
- Vue InstantSearch: version 4.22.8 or later
- InstantSearch.js
- React InstantSearch
- Vue InstantSearch
To apply the sorting strategies,
use the sortBy widget with
strategy instead of value on the items option.JavaScript