<!--
{
  "availability" : [
    "iOS: 15.0.0 -",
    "iPadOS: 15.0.0 -",
    "macCatalyst: 15.0.0 -",
    "macOS: 12.0.0 -",
    "tvOS: 15.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 8.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/KeyPathComparator/compare(_:_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "s:10Foundation17KeyPathComparatorV7compareySo18NSComparisonResultVx_xtF"
  },
  "title" : "compare(_:_:)"
}
-->

# compare(_:_:)

Provides the relative ordering of two items according to the ordering of the properties that the comparator’s key path references.

```
func compare(_ lhs: Compared, _ rhs: Compared) -> ComparisonResult
```

## Parameters

`lhs`

The first property to compare.

`rhs`

The second property to compare.

## Return Value

The relative ordering for the compared properties.

## Discussion

The method returns flipped comparisons if the sort order is [`SortOrder.reverse`](/documentation/Foundation/SortOrder/reverse).

---

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)