<!--
{
  "availability" : [
    "iOS: 8.0.0 -",
    "iPadOS: 8.0.0 -",
    "macCatalyst: 8.0.0 -",
    "macOS: 10.10.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/NSSortDescriptor/init(keyPath:ascending:comparator:)",
  "metadataVersion" : "0.1.0",
  "role" : "Initializer",
  "symbol" : {
    "kind" : "Initializer",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "s:So16NSSortDescriptorC10FoundationE7keyPath9ascending10comparatorABs03KeyE0Cyxq_G_SbSo18NSComparisonResultVyp_yptctcr0_lufc"
  },
  "title" : "init(keyPath:ascending:comparator:)"
}
-->

# init(keyPath:ascending:comparator:)

Creates a sort descriptor with a specified key path and ordering, and a comparator block.

```
convenience init<Root, Value>(keyPath: KeyPath<Root, Value>, ascending: Bool, comparator cmptr: @escaping Comparator)
```

## Parameters

`keyPath`

The key path to the property to compare.

`ascending`

If `true`, the sort descriptor compares using the [`SortOrder.forward`](/documentation/Foundation/SortOrder/forward) sort order; otherwise, it uses [`SortOrder.reverse`](/documentation/Foundation/SortOrder/reverse).

`cmptr`

A comparator block.

---

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)