---
title: "init(key:ascending:selector:)"
framework: foundation
role: symbol
role_heading: Initializer
path: "foundation/nssortdescriptor/init(key:ascending:selector:)"
---

# init(key:ascending:selector:)

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

## Declaration

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

## Parameters

- `key`: The key path for performing a comparison. For information about key paths, see 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 doc://com.apple.foundation/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 doc://com.apple.foundation/documentation/Foundation/ComparisonResult constant.

## Return Value

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

## See Also

### Creating a Sort Descriptor

- [init(key:ascending:)](foundation/nssortdescriptor/init(key:ascending:).md)
- [init(keyPath:ascending:)](foundation/nssortdescriptor/init(keypath:ascending:).md)
- [init(key:ascending:comparator:)](foundation/nssortdescriptor/init(key:ascending:comparator:).md)
- [init(keyPath:ascending:comparator:)](foundation/nssortdescriptor/init(keypath:ascending:comparator:).md)
- [init(coder:)](foundation/nssortdescriptor/init(coder:).md)
- [init(_:)](foundation/nssortdescriptor/init(_:)-7qf91.md)
