---
title: "index(of:inSortedRange:options:usingComparator:)"
framework: foundation
role: symbol
role_heading: Instance Method
path: "foundation/nsorderedset/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.

## Declaration

```swift
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 doc://com.apple.foundation/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 doc://com.apple.foundation/documentation/Foundation/NSExceptionName/rangeException.
- `opts`: Options for the search. For possible values, see doc://com.apple.foundation/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 doc://com.apple.foundation/documentation/Foundation/NSExceptionName/invalidArgumentException.

## Return Value

Return Value If the insertionIndex option is not specified: If the object is found and neither firstEqual nor lastEqual is specified, returns the matching object’s index. If the firstEqual or lastEqual option is also specified, returns the 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 object is found and neither firstEqual nor lastEqual is specified, returns the matching object’s index. If the firstEqual or  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

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.

## See Also

### Accessing Set Members

- [contains(_:)](foundation/nsorderedset/contains(_:).md)
- [enumerateObjects(at:options:using:)](foundation/nsorderedset/enumerateobjects(at:options:using:).md)
- [enumerateObjects(_:)](foundation/nsorderedset/enumerateobjects(_:).md)
- [enumerateObjects(options:using:)](foundation/nsorderedset/enumerateobjects(options:using:).md)
- [firstObject](foundation/nsorderedset/firstobject.md)
- [lastObject](foundation/nsorderedset/lastobject.md)
- [object(at:)](foundation/nsorderedset/object(at:).md)
- [subscript(_:)](foundation/nsorderedset/subscript(_:).md)
- [objects(at:)](foundation/nsorderedset/objects(at:).md)
- [index(of:)](foundation/nsorderedset/index(of:).md)
- [index(ofObjectAt:options:passingTest:)](foundation/nsorderedset/index(ofobjectat:options:passingtest:).md)
- [index(ofObjectPassingTest:)](foundation/nsorderedset/index(ofobjectpassingtest:).md)
- [index(_:ofObjectPassingTest:)](foundation/nsorderedset/index(_:ofobjectpassingtest:).md)
- [indexes(ofObjectsAt:options:passingTest:)](foundation/nsorderedset/indexes(ofobjectsat:options:passingtest:).md)
- [indexes(ofObjectsPassingTest:)](foundation/nsorderedset/indexes(ofobjectspassingtest:).md)
