<!--
{
  "availability" : [
    "iOS: 4.0.0 -",
    "iPadOS: 4.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.6.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/NSSortDescriptor/init(key:ascending:comparator:)",
  "metadataVersion" : "0.1.0",
  "role" : "Initializer",
  "symbol" : {
    "kind" : "Initializer",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSSortDescriptor(im)initWithKey:ascending:comparator:"
  },
  "title" : "init(key:ascending:comparator:)"
}
-->

# init(key:ascending:comparator:)

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

```
init(key: String?, ascending: Bool, comparator cmptr: @escaping Comparator)
```

## Parameters

`key`

The property key for performing a comparison.

    For information about key paths, see     [Key-Value Coding Programming Guide](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/KeyValueCoding/index.html#//apple_ref/doc/uid/10000107i)    .

`ascending`

<doc://com.apple.documentation/documentation/Swift/true> if the receiver specifies sorting in ascending order; otherwise, <doc://com.apple.documentation/documentation/Swift/false>.

`cmptr`

A comparator block.

## Return Value

A sort descriptor that initializes with the specified key, ordering, and comparator.

## Discussion

---

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)