<!--
{
  "availability" : [
    "iOS: 16.0.0 -",
    "iPadOS: 16.0.0 -",
    "macCatalyst: -",
    "visionOS: -"
  ],
  "documentType" : "symbol",
  "framework" : "UIKit",
  "identifier" : "/documentation/UIKit/UITextSearching-3wkjv/compare(_:toRange:document:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "UIKit"
    ],
    "preciseIdentifier" : "s:5UIKit15UITextSearchingP7compare_7toRange8documentSo18NSComparisonResultVSo0bF0C_AJ18DocumentIdentifierQzSgtF"
  },
  "title" : "compare(_:toRange:document:)"
}
-->

# compare(_:toRange:document:)

Compares ranges from the set of matches the aggregator provides to determine navigation order.

```
func compare(_ foundRange: UITextRange, toRange: UITextRange, document: Self.DocumentIdentifier?) -> ComparisonResult
```

## Parameters

`foundRange`

The range of characters in a text container to compare from.

`toRange`

The range of characters in a text container to compare to.

`document`

A string that uniquely identifies the document containing the text ranges. `Nil` when searching a single document.

## Return Value

Returns the result of comparing the two text ranges.

## Discussion

The system calls this method during a find session to determine which [`UITextRange`](/documentation/UIKit/UITextRange) to highlight next when a user taps the “next” or “previous” button.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)