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

# init(key:ascending:)

Creates a sort descriptor with a specified string key path and sort order.

```
init(key: String?, ascending: Bool)
```

## Parameters

`key`

The key path 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>.

## Return Value

A sort descriptor that initializes with the specified key path and sort order, and the default comparison selector ([compare](https://developer.apple.com/library/archive/documentation/GraphicsImaging/Reference/CIKernelLangRef/ci_gslang_ext.html#//apple_ref/doc/uid/TP40004397-CH206-BCIECEBI)).

---

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)