Concepts / Building Search UI / Sorting Results
May. 10, 2019

Sorting Results

Sorting Results

When browsing through items, it’s always good to give the user the option to sort his results by a certain attribute such as price. This can easily be done by using InstantSearch.

Example

Let’s say you have a product page and you have 2 indices: your main index products and your products sorted by price products_price_asc (refer to our Sorting guide to see how this is done). We will use the power of multi-indexing in order to achieve this.

In AppDelegate.swift, when initializing InstantSearch, add the indices:

1
2
3
4
5
let searcherIds: [SearcherId] = [
  SearcherId.init(index: "products"),
  SearcherId.init(index: "products_price_asc")
]
InstantSearch.shared.configure(appID: ALGOLIA_APP_ID, apiKey: ALGOLIA_API_KEY, searcherIds: searcherIds)

Then, when the user decides to sort some hitsTableView for example, do the following:

1
2
hitsTableView.index = "products_price_asc"
InstantSearch.shared.register(widget: hitsTableView, doSearch: true)

Did you find this page helpful?

iOS InstantSearch v3