<!--
{
  "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:selector:)",
  "metadataVersion" : "0.1.0",
  "role" : "Initializer",
  "symbol" : {
    "kind" : "Initializer",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSSortDescriptor(im)initWithKey:ascending:selector:"
  },
  "title" : "init(key:ascending:selector:)"
}
-->

# init(key:ascending:selector:)

Creates a sort descriptor with a specified string key path, ordering, and comparison selector.

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

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

`selector`

The method to use when comparing the properties of objects, such as [`localizedStandardCompare(_:)`](/documentation/Foundation/NSString/localizedStandardCompare(_:)). The selector must specify a method that you implement according to the value of the property that the key path identifies. Pass the selector a single parameter, the object to compare against, and it returns the appropriate [`ComparisonResult`](/documentation/Foundation/ComparisonResult) constant.

## Return Value

A sort descriptor that initializes with the specified key path, sort order, and comparison selector.

---

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)