> ## 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.

```swift Signature theme={"system"}
FilterComparisonConnector<Number: Comparable & DoubleRepresentable>(
  searcher: HitsSearcher,
  filterState: FilterState,
  attribute: Attribute,
  numericOperator: Filter.Numeric.Operator,
  number: Number?,
  bounds: ClosedRange<Number>?,
  operator: RefinementOperator,
  groupName: String?
)
```

<Card title="Explore example code" icon="github" href="https://github.com/algolia/instantsearch-ios/tree/master/Examples/Showcase">
  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

Instantiate a `FilterComparisonConnector` and launch an initial search on its searcher.

```swift Swift icon=code theme={"system"}
let searcher = HitsSearcher(appID: "YourApplicationID",
                            apiKey: "YourApiKey",
                            indexName: "YourIndexName")
let filterState = FilterState()

let stepperController = NumericStepperController()

let filterComparisonConnector = FilterComparisonConnector<Double>(searcher: searcher,
                                                                  filterState: filterState,
                                                                  numericOperator: .greaterThanOrEqual,
                                                                  attribute: "price",
                                                                  controller: stepperController)

searcher.search()
```

### 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/ios) that handles your searches.
* `FilterState`. The current state of the filters.
* `NumberInteractor<T>`. The logic applied to the numeric value.
* `NumberController`. The view controller that renders the numeric value.

```swift Swift icon=code theme={"system"}
let searcher = HitsSearcher(appID: "YourApplicationID",
                            apiKey: "YourApiKey",
                            indexName: "YourIndexName")
let filterState = FilterState()
let numberInteractor = NumberInteractor<Double>()
let stepperController = NumericStepperController()

interactor.connectSearcher(searcher, attribute: "price")
interactor.connectFilterState(filterState,
                              attribute: attribute,
                              numericOperator: numericOperator,
                              operator: .greaterThanOrEqual)
interactor.connectNumberController(stepperController)

searcher.search()
```

### SwiftUI

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

```swift Swift icon=code theme={"system"}
struct ContentView: View {

  @ObservedObject var priceController: NumberObservableController<Double>

  var body: some View {
    HStack {
      Stepper(value: $priceController.value,
              in: priceController.bounds,
              step: 0.1) {
        HStack {
          Text("Price:")
          Spacer()
          Text(String(format: "%.2f", priceController.value))
        }
      }
    }
  }

}
```

If you prefer to create a custom SwiftUI view that presents the numeric range,
you can directly use the `NumberObservableController` as a data model.
It provides `value` and `bounds` properties to streamline the design process of your custom SwiftUI view.

## Parameters

<ParamField body="searcher" type="HitsSearcher" required>
  The Searcher that handles your searches
</ParamField>

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

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

<ParamField body="numericOperator" type="Filter.Numeric.Operator" required>
  The comparison operator to apply.
</ParamField>

<ParamField body="number" type="Number: Comparable & DoubleRepresentable" post={["default: nil"]}>
  The initial numeric value to filter on.
</ParamField>

<ParamField body="bounds" type="ClosedRange<Number>?" post={["default: nil"]}>
  The optional bounds limiting the max and the min value of the number.
</ParamField>

<ParamField body="operator" type="RefinementOperator" post={["default: .and"]} required>
  Whether the filter is added to a conjuncitve(`and`) or  a disjuncitve (`or`) group in the filter state.
</ParamField>

<ParamField body="groupName" type="String?" post={["default: nil"]}>
  Filter group name in the filter state.
  If not specified, the attribute value is used as the group name.
</ParamField>

## Controller

<ParamField body="controller" type="NumberController" required>
  Controller interfacing with a concrete number view.

  ```swift Swift icon=code theme={"system"}
  let controller = MyNumberController() // your `NumberController` implementation
  filterComparisonConnector.connectNumberController(controller)
  ```
</ParamField>
