<!--
{
  "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",
  "metadataVersion" : "0.1.0",
  "role" : "Structure",
  "symbol" : {
    "kind" : "Structure",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "s:10Foundation17KeyPathComparatorV"
  },
  "title" : "KeyPathComparator"
}
-->

# KeyPathComparator

A comparator that uses another sort comparator to provide the comparison of values at a key path.

```
struct KeyPathComparator<Compared>
```

## Topics

### Using Key Path Comparators

[`compare(_:_:)`](/documentation/Foundation/KeyPathComparator/compare(_:_:))

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

### Inspecting Key Path Comparators

[`keyPath`](/documentation/Foundation/KeyPathComparator/keyPath)

The key path that the comparator uses to compare properties.

[`order`](/documentation/Foundation/KeyPathComparator/order)

The sort order that the comparator uses to compare properties.

### Initializers

[`init(_:comparator:)`](/documentation/Foundation/KeyPathComparator/init(_:comparator:)-8b13q)

[`init(_:comparator:)`](/documentation/Foundation/KeyPathComparator/init(_:comparator:)-284rt)

[`init(_:comparator:order:)`](/documentation/Foundation/KeyPathComparator/init(_:comparator:order:)-749jk)

[`init(_:comparator:order:)`](/documentation/Foundation/KeyPathComparator/init(_:comparator:order:)-3gjxd)

[`init(_:order:)`](/documentation/Foundation/KeyPathComparator/init(_:order:)-6r8gw)

[`init(_:order:)`](/documentation/Foundation/KeyPathComparator/init(_:order:)-4hyoi)



---

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)