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

# Filter Numeric Comparison

> Lets users refine search results based on numeric comparison.

```kotlin Signature theme={"system"}
FilterComparisonConnector<T>(
    filterState: FilterState,
    attribute: String,
    operator: NumericOperator,
    number: T?,
    groupID: FilterGroupID
) where T : Number, T : Comparable<T>
```

<Card title="Explore example code" icon="github" href="https://github.com/algolia/instantsearch-android/tree/master/examples/android">
  Browse the Filter Numeric Comparison example code on GitHub.
</Card>

## About this widget

Filter Numeric Comparison is a view to filter on a numeric value using a comparison operator.

## Examples

```kotlin Kotlin icon=code theme={"system"}
class MyActivity : AppCompatActivity() {
    val searcher = HitsSearcher(
        applicationID = "YourApplicationID",
        apiKey = "YourSearchOnlyAPIKey",
        indexName = "YourIndexName"
    )
    val filterState = FilterState()
    val price = "price"
    val operator = NumericOperator.GreaterOrEquals
    val filterComparison = FilterComparisonConnector<Long>(
        filterState = filterState,
        attribute = price,
        operator = operator
    )
    val connection = ConnectionHandler(filterComparison, searcher.connectFilterState(filterState))

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val priceView: NumberView<Long> = MyFilterPriceView() // your `NumberView<T>` implementation
        connection += filterComparison.connectView(priceView)
        searcher.searchAsync()
    }

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

### Low-level API

If you want to fully control the Filter Numeric Comparison components and connect them manually, use the following components:

* `Searcher`. The [`Searcher`](/doc/api-reference/widgets/instantsearch/android) that handles your searches.
* `FilterState`. The current state of the filters.
* `NumberViewModel<T>`. The logic applied to the numeric value.
* `NumberView<T>`. The view that renders the numeric value.

```kotlin Kotlin icon=code theme={"system"}
class MyActivity : AppCompatActivity() {
    val searcher = HitsSearcher(
        applicationID = "YourApplicationID",
        apiKey = "YourSearchOnlyAPIKey",
        indexName = "YourIndexName"
    )
    val filterState = FilterState()
    val price = "price"
    val operator = NumericOperator.Greater
    val viewModel = NumberViewModel(range = 0..10)
    val connection = ConnectionHandler()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val view: NumberView<Int> = MyFilterPriceView() // your `NumberView<T>` implementation
        searcher.query = searcher.query.copy(facets = (searcher.query.facets ?: emptyList()) + listOf(price))
        connection += searcher.connectFilterState(filterState)
        connection += viewModel.connectView(view)
        connection += viewModel.connectFilterState(filterState, price, operator)

        searcher.searchAsync()
    }

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

### Compose UI

InstantSearch provides the `NumberState` as a state model,
which is an implementation of the `NumberView` interface.
You need to connect `NumberState` to the `FilterComparisonConnector` or `NumberViewModel` like any other `NumberView` implementation.

```kotlin Kotlin icon=code theme={"system"}
class MyActivity : AppCompatActivity() {
    val searcher = HitsSearcher(
        applicationID = "YourApplicationID",
        apiKey = "YourSearchOnlyAPIKey",
        indexName = "YourIndexName"
    )
    val filterState = FilterState()
    val price = "price"
    val priceState = NumberState<Long>()
    val filterComparison = FilterComparisonConnector<Long>(
        filterState = filterState,
        attribute = price,
        operator = NumericOperator.GreaterOrEquals
    )
    val connections = ConnectionHandler(filterComparison)

    init {
        connections += searcher.connectFilterState(filterState)
        connections += filterComparison.connectView(priceState)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MyFilterPrice(priceState) // your own UI composable to display `text` and `computation`
        }
        searcher.searchAsync()
    }

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

## Parameters

<ParamField body="filterState" type="FilterState" required>
  The [`FilterState`](/doc/api-reference/widgets/filter-state/android) that holds filters.
</ParamField>

<ParamField body="attribute" type="String" required>
  The attribute to filter on.
</ParamField>

<ParamField body="operator" type="NumericOperator" required>
  The `NumericOperator` used to perform a numeric comparison.
</ParamField>

<ParamField body="number" type="T : Number, T : Comparable<T>" post={["default: null"]}>
  Initial numeric value to filter on.
</ParamField>

<ParamField body="groupID" type="FilterGroupID" post={["default: FilterGroupID(attribute, FilterOperator.And)"]}>
  Groups all created filters under an ID and composes them with this operator.
  Defaults to the used attribute, with `FilterOperator.And` between filters in this group.
</ParamField>

## View

<ParamField body="view" type="NumberView<T>" required>
  The view that renders the numeric value.
</ParamField>

<ParamField body="presenter" type="NumberPresenter<T>" default="NumberPresenterImpl">
  How to display the numeric value.

  ```kotlin Kotlin icon=code theme={"system"}
  val view: NumberView<Int> = MyFilterYearView() // your `NumberView<T>` implementation
  filterNumericComparison.connectView(view) { year -> year?.toString() ?: "" }
  ```
</ParamField>
