<!--
{
  "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/NSIndexPath/compare(_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSIndexPath(im)compare:"
  },
  "title" : "compare(_:)"
}
-->

# compare(_:)

Indicates the depth-first traversal order of the receiving index path and another index path.

```
func compare(_ otherObject: IndexPath) -> ComparisonResult
```

## Parameters

`otherObject`

Index path to compare.

    This value must not be     `nil`    . If the value is     `nil`    , the behavior is undefined.

## Return Value

The depth-first traversal ordering of the receiving index path and `indexPath`.

## Discussion

- [`ComparisonResult.orderedAscending`](/documentation/Foundation/ComparisonResult/orderedAscending): The receiving index path comes before `indexPath`.
- [`ComparisonResult.orderedDescending`](/documentation/Foundation/ComparisonResult/orderedDescending): The receiving index path comes after `indexPath`.
- [`ComparisonResult.orderedSame`](/documentation/Foundation/ComparisonResult/orderedSame): The receiving index path and `indexPath` are the same index path.

---

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)