<!--
{
  "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",
  "metadataVersion" : "0.1.0",
  "role" : "Structure",
  "symbol" : {
    "kind" : "Structure",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "s:10Foundation14SortDescriptorV"
  },
  "title" : "SortDescriptor"
}
-->

# SortDescriptor

A serializable description of how to sort numerics and strings.

```
struct SortDescriptor<Compared>
```

## Topics

### Creating Sort Descriptors

[`init?(NSSortDescriptor, comparing: Compared.Type)`](/documentation/Foundation/SortDescriptor/init(_:comparing:))

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

### Using Sort Descriptors

[`func compare(Compared, Compared) -> ComparisonResult`](/documentation/Foundation/SortDescriptor/compare(_:_:))

Provides the relative ordering of two elements.

### Inspecting Sort Descriptors

[`var order: SortOrder`](/documentation/Foundation/SortDescriptor/order)

The sort order that the sort descriptor uses to compare.

### Initializers

[`init(any KeyPath<Compared, String?> & Sendable, comparator: String.StandardComparator)`](/documentation/Foundation/SortDescriptor/init(_:comparator:)-16bsg)

[`init(any KeyPath<Compared, String> & Sendable, comparator: String.StandardComparator)`](/documentation/Foundation/SortDescriptor/init(_:comparator:)-1xorc)

[`init(any KeyPath<Compared, String> & Sendable, comparator: String.StandardComparator)`](/documentation/Foundation/SortDescriptor/init(_:comparator:)-7vg3x)

[`init(any KeyPath<Compared, String?> & Sendable, comparator: String.StandardComparator)`](/documentation/Foundation/SortDescriptor/init(_:comparator:)-9m8l9)

[`init(any KeyPath<Compared, String> & Sendable, comparator: String.StandardComparator, order: SortOrder)`](/documentation/Foundation/SortDescriptor/init(_:comparator:order:)-2ouai)

[`init(any KeyPath<Compared, String> & Sendable, comparator: String.StandardComparator, order: SortOrder)`](/documentation/Foundation/SortDescriptor/init(_:comparator:order:)-4qaip)

[`init(any KeyPath<Compared, String?> & Sendable, comparator: String.StandardComparator, order: SortOrder)`](/documentation/Foundation/SortDescriptor/init(_:comparator:order:)-76h8b)

[`init(any KeyPath<Compared, String?> & Sendable, comparator: String.StandardComparator, order: SortOrder)`](/documentation/Foundation/SortDescriptor/init(_:comparator:order:)-pz7l)

[`init(any KeyPath<Compared, Double> & Sendable, order: SortOrder)`](/documentation/Foundation/SortDescriptor/init(_:order:)-1t1a5)

[`init(any KeyPath<Compared, UInt?> & Sendable, order: SortOrder)`](/documentation/Foundation/SortDescriptor/init(_:order:)-29e6k)

[`init(any KeyPath<Compared, Float?> & Sendable, order: SortOrder)`](/documentation/Foundation/SortDescriptor/init(_:order:)-29pto)

[`init(any KeyPath<Compared, Int8> & Sendable, order: SortOrder)`](/documentation/Foundation/SortDescriptor/init(_:order:)-2u61k)

[`init(any KeyPath<Compared, Int32?> & Sendable, order: SortOrder)`](/documentation/Foundation/SortDescriptor/init(_:order:)-3fgjr)

[`init(any KeyPath<Compared, UUID?> & Sendable, order: SortOrder)`](/documentation/Foundation/SortDescriptor/init(_:order:)-3iwfh)

[`init(any KeyPath<Compared, UInt64?> & Sendable, order: SortOrder)`](/documentation/Foundation/SortDescriptor/init(_:order:)-3wlt2)

[`init<Value>(any KeyPath<Compared, Value?> & Sendable, order: SortOrder)`](/documentation/Foundation/SortDescriptor/init(_:order:)-3wozy)

[`init(any KeyPath<Compared, UInt16> & Sendable, order: SortOrder)`](/documentation/Foundation/SortDescriptor/init(_:order:)-49ozr)

[`init(any KeyPath<Compared, Int16> & Sendable, order: SortOrder)`](/documentation/Foundation/SortDescriptor/init(_:order:)-4b7jd)

[`init<Value>(any KeyPath<Compared, Value> & Sendable, order: SortOrder)`](/documentation/Foundation/SortDescriptor/init(_:order:)-4doe9)

[`init(any KeyPath<Compared, Double?> & Sendable, order: SortOrder)`](/documentation/Foundation/SortDescriptor/init(_:order:)-4uo4r)

[`init(any KeyPath<Compared, UUID> & Sendable, order: SortOrder)`](/documentation/Foundation/SortDescriptor/init(_:order:)-4z0c9)

[`init(any KeyPath<Compared, Int32> & Sendable, order: SortOrder)`](/documentation/Foundation/SortDescriptor/init(_:order:)-51msp)

[`init(any KeyPath<Compared, UInt64> & Sendable, order: SortOrder)`](/documentation/Foundation/SortDescriptor/init(_:order:)-52see)

[`init(any KeyPath<Compared, UInt32?> & Sendable, order: SortOrder)`](/documentation/Foundation/SortDescriptor/init(_:order:)-5lbot)

[`init(any KeyPath<Compared, Int> & Sendable, order: SortOrder)`](/documentation/Foundation/SortDescriptor/init(_:order:)-5myfn)

[`init(any KeyPath<Compared, Bool> & Sendable, order: SortOrder)`](/documentation/Foundation/SortDescriptor/init(_:order:)-5s8d4)

[`init(any KeyPath<Compared, Int64?> & Sendable, order: SortOrder)`](/documentation/Foundation/SortDescriptor/init(_:order:)-5y1wt)

[`init(any KeyPath<Compared, Date?> & Sendable, order: SortOrder)`](/documentation/Foundation/SortDescriptor/init(_:order:)-7rdjb)

[`init(any KeyPath<Compared, Bool?> & Sendable, order: SortOrder)`](/documentation/Foundation/SortDescriptor/init(_:order:)-86gfd)

[`init(any KeyPath<Compared, UInt32> & Sendable, order: SortOrder)`](/documentation/Foundation/SortDescriptor/init(_:order:)-8flg7)

[`init(any KeyPath<Compared, Float> & Sendable, order: SortOrder)`](/documentation/Foundation/SortDescriptor/init(_:order:)-8jc9k)

[`init(any KeyPath<Compared, UInt> & Sendable, order: SortOrder)`](/documentation/Foundation/SortDescriptor/init(_:order:)-8tm2c)

[`init(any KeyPath<Compared, UInt8> & Sendable, order: SortOrder)`](/documentation/Foundation/SortDescriptor/init(_:order:)-95o7r)

[`init(any KeyPath<Compared, Int16?> & Sendable, order: SortOrder)`](/documentation/Foundation/SortDescriptor/init(_:order:)-9noh7)

[`init(any KeyPath<Compared, Date> & Sendable, order: SortOrder)`](/documentation/Foundation/SortDescriptor/init(_:order:)-9xg3w)

[`init(any KeyPath<Compared, UInt16?> & Sendable, order: SortOrder)`](/documentation/Foundation/SortDescriptor/init(_:order:)-ks7r)

[`init(any KeyPath<Compared, UInt8?> & Sendable, order: SortOrder)`](/documentation/Foundation/SortDescriptor/init(_:order:)-kwgp)

[`init(any KeyPath<Compared, Int?> & Sendable, order: SortOrder)`](/documentation/Foundation/SortDescriptor/init(_:order:)-qlnj)

[`init(any KeyPath<Compared, Int64> & Sendable, order: SortOrder)`](/documentation/Foundation/SortDescriptor/init(_:order:)-rnot)

[`init(any KeyPath<Compared, Int8?> & Sendable, order: SortOrder)`](/documentation/Foundation/SortDescriptor/init(_:order:)-z7th)

### Instance Properties

[`var keyPath: PartialKeyPath<Compared>?`](/documentation/Foundation/SortDescriptor/keyPath)

The key path to the field for comparison.

[`var stringComparator: String.StandardComparator?`](/documentation/Foundation/SortDescriptor/stringComparator)

A `String.StandardComparator` value.

## Relationships

### Conforms To

[`Decodable`](/documentation/Swift/Decodable)

[`Equatable`](/documentation/Swift/Equatable)

[`Hashable`](/documentation/Swift/Hashable)

[`Encodable`](/documentation/Swift/Encodable)

[`SortComparator`](/documentation/Foundation/SortComparator)

[`SendableMetatype`](/documentation/Swift/SendableMetatype)

[`Sendable`](/documentation/Swift/Sendable)

---

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)