---
title: "init(keyPath:ascending:comparator:)"
framework: foundation
role: symbol
role_heading: Initializer
path: "foundation/nssortdescriptor/init(keypath:ascending:comparator:)"
---

# init(keyPath:ascending:comparator:)

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

## Declaration

```swift
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 doc://com.apple.foundation/documentation/Foundation/SortOrder/forward sort order; otherwise, it uses doc://com.apple.foundation/documentation/Foundation/SortOrder/reverse.
- `cmptr`: A comparator block.

## See Also

### Creating a Sort Descriptor

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