> ## Documentation Index
> Fetch the complete documentation index at: https://algolia.com/llms.txt
> Use this file to discover all available pages before exploring further.

# SortBy

> Shows a list of indices for alternative sorting or ranking of search results.

export const Index = () => <Tooltip tip="An Algolia index is a searchable dataset that consists of records and configuration settings. These settings define how the records are searched and ranked.">
    index
  </Tooltip>;

```kotlin Signature theme={"system"}
SortByConnector(
    indexes: Map<Int, String>
    searcher: HitsSearcher
    selected: Int?
)
```

<Card title="Explore example code" icon="github" href="https://github.com/algolia/instantsearch-android/tree/master/examples/android/src/main/kotlin/com/algolia/instantsearch/examples/android/showcase/androidview/sortby">
  Browse the SortBy example code on GitHub.
</Card>

## About this widget

This widget displays a list of indices,
allowing a user to change the way hits are sorting
(with [replica indices](/doc/guides/managing-results/refine-results/sorting/how-to/creating-replicas)).
Another common use case for this widget is to let users switch between different indices.

For this widget to work,
you must define all indices that you pass down as options as replicas of the main <Index />.

## Examples

```kotlin Kotlin icon=code theme={"system"}
class MyActivity : AppCompatActivity() {

  val indexTitle = "mobile_demo_movies"
  val indexYearAsc = "mobile_demo_movies_year_asc"
  val indexYearDesc = "mobile_demo_movies_year_desc"
  val searcher = HitsSearcher(
      applicationID = "YourApplicationID",
      apiKey = "YourSearchOnlyAPIKey",
      indexName = indexTitle
  )
  val indexes = mapOf(
      0 to indexTitle,
      1 to indexYearAsc,
      2 to indexYearDesc
  )
  val sortBy = SortByConnector(searcher, indexes, selected = 0)
  val connection = ConnectionHandler(sortBy)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val adapter = ArrayAdapter<String>(this, R.layout.menu_item)
        val view = SortByViewAutocomplete(autoCompleteTextView, adapter)
        connection += sortBy.connectView(view) { indexName ->
            when (indexName) {
                indexTitle -> "Default"
                indexYearAsc -> "Year Asc"
                indexYearDesc -> "Year Desc"
                else -> indexName
            }
        }
        searcher.searchAsync()
    }

    override fun onDestroy() {
        super.onDestroy()
        searcher.cancel()
        connection.disconnect()
    }
}
```

### Low-level API

To add `SortBy` to your search experience, use these components:

* `Searcher`. The [`Searcher`](/doc/api-reference/widgets/instantsearch/android) that handles your searches.
* `SortByViewModel`. The logic applied to the index sorting/switching.
* `SortByViewAutocomplete`. The concrete view implementing SortByView.
* `IndexPresenter`. Optional. The presenter that converts an Index to a String output.

```kotlin Kotlin icon=code theme={"system"}
class MyActivity : AppCompatActivity() {
    val indexTitle = "mobile_demo_movies"
    val indexYearAsc = "mobile_demo_movies_year_asc"
    val indexYearDesc = "mobile_demo_movies_year_desc"
    val searcher = HitsSearcher(
        applicationID = "YourApplicationID",
        apiKey = "YourSearchOnlyAPIKey",
        indexName = indexTitle
    )
    val indexes = mapOf(
        0 to indexTitle,
        1 to indexYearAsc,
        2 to indexYearDesc
    )
    val viewModel = SortByViewModel(map = indexes, selected = 0)
    val connection = ConnectionHandler()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val adapter = ArrayAdapter<String>(this, R.layout.menu_item)
        val view = SortByViewAutocomplete(autoCompleteTextView, adapter)

        connection += viewModel.connectSearcher(searcher)
        connection += viewModel.connectView(view){ indexName ->
            when (indexName) {
                indexTitle -> "Default"
                indexYearAsc -> "Year Asc"
                indexYearDesc -> "Year Desc"
                else -> indexName
            }
        }
        searcher.searchAsync()
    }

    override fun onDestroy() {
        super.onDestroy()
        searcher.cancel()
        connection.disconnect()
    }
}
```

### Compose UI

```kotlin Kotlin icon=code theme={"system"}
class MyActivity : AppCompatActivity() {
    private val movies = "mobile_demo_movies"
    private val moviesAsc = "mobile_demo_movies_year_asc"
    private val moviesDesc = "mobile_demo_movies_year_desc"
    private val hitsState = HitsState<Movie>()
    private val indexes = mapOf(
        0 to movies,
        1 to moviesAsc,
        2 to moviesDesc
    )
    private val searcher = HitsSearcher(
        applicationID = "YourApplicationID",
        apiKey = "YourSearchOnlyAPIKey",
        indexName = movies
    )
    private val sortByState = SortByState()
    private val sortBy = SortByConnector(searcher, indexes, selected = 0)
    private val connections = ConnectionHandler(sortBy)

    init {
        connections += searcher.connectHitsView(hitsState) { it.hits.deserialize(Movie.serializer()) }
        connections += sortBy.connectView(sortByState) { indexName ->
            when (indexName) {
                movies -> "Default"
                moviesAsc -> "Year Asc"
                moviesDesc -> "Year Desc"
                else -> indexName
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MySortByScreen() // your own UI composable to display sorting options
        }
        searcher.searchAsync()
    }

    override fun onDestroy() {
        super.onDestroy()
        searcher.cancel()
        connections.clear()
    }
}
```

## Parameters

<ParamField body="indexes" type="Map<Int, String>" required>
  The list of indices to search in.
</ParamField>

<ParamField body="searcher" type="HitsSearcher" required>
  The [`Searcher`](/doc/api-reference/widgets/instantsearch/android) that handles your searches.
</ParamField>

<ParamField body="selected" type="Int?" post={["default: null"]}>
  The index to select. By default, none is selected.
</ParamField>

## View

<ParamField body="view" type="SortByView" required>
  The view that renders the list of indices.

  InstantSearch includes the `SortByViewSpinner` and `SortByViewAutocomplete` implementations.
</ParamField>

<ParamField body="presenter" type="IndexPresenter" default="IndexPresenterImpl">
  The presenter that defines the way we want to display an index,
  taking as input an Index and returning a String.

  ```kotlin Kotlin icon=code theme={"system"}
  val adapter = ArrayAdapter<String>(this, R.layout.menu_item) // `ArrayAdapter` implementation
  val view = SortByViewAutocomplete(autoCompleteTextView, adapter)
  sortBy.connectView(view) { index ->
      when (index) {
          indexTitle -> "Default"
          indexYearAsc -> "Year Asc"
          indexYearDesc -> "Year Desc"
          else -> index
      }
  }
  ```
</ParamField>
