---
title: "index(of:inSortedRange:options:usingComparator:)"
framework: foundation
role: symbol
role_heading: Instance Method
path: "foundation/nsarray/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 array using a given NSComparator block.

## Declaration

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

## Parameters

- `obj`: An object for which to search in the array. If this value is nil, throws an doc://com.apple.foundation/documentation/Foundation/NSExceptionName/invalidArgumentException.
- `r`: The range within the array to search for obj. If r exceeds the bounds of the array (if the location plus length of the range is greater than the count of the array), throws an doc://com.apple.foundation/documentation/Foundation/NSExceptionName/rangeException.
- `opts`: Options for the search. For possible values, see doc://com.apple.foundation/documentation/Foundation/NSBinarySearchingOptions. If you specify both doc://com.apple.foundation/documentation/Foundation/NSBinarySearchingOptions/firstEqual and doc://com.apple.foundation/documentation/Foundation/NSBinarySearchingOptions/lastEqual, throws an NSInvalidArgumentException.
- `cmp`: A comparator block used to compare the object obj with elements in the array. If this value is NULL, throws an doc://com.apple.foundation/documentation/Foundation/NSExceptionName/invalidArgumentException.

## Return Value

Return Value If the insertionIndex option is not specified: If the obj is found and neither firstEqual nor lastEqual is specified, returns an arbitrary matching object’s index. If the firstEqual option is also specified, returns the lowest index of equal objects. If the lastEqual option is also specified, returns the highest index of equal objects. If the object is not found, returns NSNotFound. If the insertionIndex option is specified, returns the index at which you should insert obj in order to maintain a sorted array: If the obj is found and neither firstEqual nor lastEqual is specified, returns any equal or one larger index than any matching object’s index. If the firstEqual option is also specified, returns the lowest index of equal objects. If the lastEqual option is also specified, returns the highest index of 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

Discussion The elements in the array must have already been sorted using the comparator cmp.  If the array is not sorted, the result is undefined.

## See Also

### Finding Objects in an Array

- [index(of:)](foundation/nsarray/index(of:).md)
- [index(of:in:)](foundation/nsarray/index(of:in:).md)
- [indexOfObjectIdentical(to:)](foundation/nsarray/indexofobjectidentical(to:).md)
- [indexOfObjectIdentical(to:in:)](foundation/nsarray/indexofobjectidentical(to:in:).md)
- [indexOfObject(passingTest:)](foundation/nsarray/indexofobject(passingtest:).md)
- [indexOfObject(options:passingTest:)](foundation/nsarray/indexofobject(options:passingtest:).md)
- [indexOfObject(at:options:passingTest:)](foundation/nsarray/indexofobject(at:options:passingtest:).md)
- [indexesOfObjects(passingTest:)](foundation/nsarray/indexesofobjects(passingtest:).md)
- [indexesOfObjects(options:passingTest:)](foundation/nsarray/indexesofobjects(options:passingtest:).md)
- [indexesOfObjects(at:options:passingTest:)](foundation/nsarray/indexesofobjects(at:options:passingtest:).md)
