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 an object 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.
Sort multifeed compositions
In a multifeed composition, you can applysortBy to the whole composition or to individual feeds:
- Per composition: send
sortByin the rootparams. Feeds inherit it unless they set their ownsortBy. - Per feed: send
sortByinfeeds.<feedID>.params. The feed value replaces the root value.
JSON
- To turn sorting off for a feed that would otherwise inherit the root
sortBy, set"sortBy": ""in the feed’sparams. - When the composition runs, the API ignores a sorting index that’s unrelated to the feed’s indices and reports it in the
errorsproperty. The API also ignores invalidsortByvalues and reports them in theerrorsproperty. - Rules that trigger on
sortBymatch only the root value. AsortByvalue set only for a feed doesn’t trigger rules.
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