<!--
{
  "availability" : [
    "iOS: 2.0.0 -",
    "iPadOS: 2.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.0.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/NSOrderedSet/index(of:inSortedRange:options:usingComparator:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSOrderedSet(im)indexOfObject:inSortedRange:options:usingComparator:"
  },
  "title" : "index(of:inSortedRange:options:usingComparator:)"
}
-->

# index(of:inSortedRange:options:usingComparator:)

Returns the index, within a specified range, of an object compared with elements in the ordered set using a given NSComparator block.

```
func index(of object: Any, inSortedRange range: NSRange, options opts: NSBinarySearchingOptions = [], usingComparator cmp: (Any, Any) -> ComparisonResult) -> Int
```

## Parameters

`object`

An object for which to search in the ordered set.

    If this value is     `nil`    , throws an [`invalidArgumentException`](/documentation/Foundation/NSExceptionName/invalidArgumentException)    .

`range`

The range within the array to search for `object`.

    If r exceeds the bounds of the ordered set (if the location plus length of the range is greater than the count of the ordered set), throws an [`rangeException`](/documentation/Foundation/NSExceptionName/rangeException)    .

`opts`

Options for the search. For possible values, see [`NSBinarySearchingOptions`](/documentation/Foundation/NSBinarySearchingOptions).

`cmp`

A comparator block used to compare the object obj with elements in the ordered set.

    If this value is     `NULL`    , throws an [`invalidArgumentException`](/documentation/Foundation/NSExceptionName/invalidArgumentException)    .

## Return Value

If the [`insertionIndex`](/documentation/Foundation/NSBinarySearchingOptions/insertionIndex) option is not specified:

- If the `object` is found and neither [`firstEqual`](/documentation/Foundation/NSBinarySearchingOptions/firstEqual) nor [`lastEqual`](/documentation/Foundation/NSBinarySearchingOptions/lastEqual) is specified, returns the matching object’s index.
- If the [`firstEqual`](/documentation/Foundation/NSBinarySearchingOptions/firstEqual) or [`lastEqual`](/documentation/Foundation/NSBinarySearchingOptions/lastEqual) option is also specified, returns the index of equal objects.
- If the object is not found, returns `NSNotFound`.

  If the [`insertionIndex`](/documentation/Foundation/NSBinarySearchingOptions/insertionIndex)   option is specified, returns the index at which you should insert   `obj`   in order to maintain a sorted array:

- If the `object` is found and neither [`firstEqual`](/documentation/Foundation/NSBinarySearchingOptions/firstEqual) nor [`lastEqual`](/documentation/Foundation/NSBinarySearchingOptions/lastEqual) is specified, returns the matching object’s index.
- If the [`firstEqual`](/documentation/Foundation/NSBinarySearchingOptions/firstEqual) or  [`lastEqual`](/documentation/Foundation/NSBinarySearchingOptions/lastEqual) option is also specified, returns the index of the equal objects.
- If the object is not found, returns the index of the least greater object, or the index at the end of the array if the object is larger than all other elements.

## Discussion

The elements in the ordered set  must have already been sorted using the comparator `cmp`. If the ordered set is not sorted, the result is undefined.

---

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)