<!--
{
  "availability" : [
    "iOS: 15.0.0 -",
    "iPadOS: 15.0.0 -",
    "macCatalyst: 15.0.0 -",
    "macOS: 12.0.0 -",
    "tvOS: 15.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 8.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/SortDescriptor/init(_:comparing:)",
  "metadataVersion" : "0.1.0",
  "role" : "Initializer",
  "symbol" : {
    "kind" : "Initializer",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "s:10Foundation14SortDescriptorV_9comparingACyxGSgSo06NSSortC0C_xmtcSo8NSObjectCRbzlufc"
  },
  "title" : "init(_:comparing:)"
}
-->

# init(_:comparing:)

Creates a sort descriptor using a sort descriptor and a type that you specify.

```
init?(_ descriptor: NSSortDescriptor, comparing comparedType: Compared.Type) where Compared : NSObject
```

## Parameters

`descriptor`

A sort descriptor.

`comparedType`

The type that the sort descriptor compares.

## Discussion

Returns `nil` if there isn’t a [`SortDescriptor`](/documentation/Foundation/SortDescriptor) equivalent to the [`NSSortDescriptor`](/documentation/Foundation/NSSortDescriptor) you specify, or if the selector to [`NSSortDescriptor`](/documentation/Foundation/NSSortDescriptor) isn’t one of the standard string comparison algorithms or `compare(_:)`.

The comparison for the created [`SortDescriptor`](/documentation/Foundation/SortDescriptor) uses the selector to the associated [`NSSortDescriptor`](/documentation/Foundation/NSSortDescriptor) directly, so in cases where the comparison of [`NSSortDescriptor`](/documentation/Foundation/NSSortDescriptor) might crash, the [`SortDescriptor`](/documentation/Foundation/SortDescriptor) comparison crashes as well.

---

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)