<!--
{
  "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/ComparableComparator/compare(_:_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "s:10Foundation20ComparableComparatorV7compareySo18NSComparisonResultVx_xtF"
  },
  "title" : "compare(_:_:)"
}
-->

# compare(_:_:)

Provides the relative ordering of two elements.

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

## Parameters

`lhs`

The first element to compare.

`rhs`

The second element to compare.

## Return Value

The relative ordering between the two elements.

## 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)