<!--
{
  "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",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSSortDescriptor"
  },
  "title" : "NSSortDescriptor"
}
-->

# NSSortDescriptor

An immutable description of how to order a collection of objects according to a property common to all the objects.

```
class NSSortDescriptor
```

## Overview

You construct instances of [`NSSortDescriptor`](/documentation/Foundation/NSSortDescriptor) by specifying the key path of the property to compare and the order of the sort (ascending or descending). Optionally, you can also specify a selector to use to perform the comparison, which allows you to specify other comparison selectors, such as [`localizedStandardCompare(_:)`](/documentation/Foundation/NSString/localizedStandardCompare(_:)) and [`localizedCaseInsensitiveCompare(_:)`](/documentation/Foundation/NSString/localizedCaseInsensitiveCompare(_:)). Sorting raises an exception if the objects don’t respond to the sort descriptor’s comparison selector.

You can use sort descriptors for the following:

- Sorting an array (an instance of [`NSArray`](/documentation/Foundation/NSArray) or [`NSMutableArray`](/documentation/Foundation/NSMutableArray) — see [`sortedArray(using:)`](/documentation/Foundation/NSArray/sortedArray(using:)-82wi1) and [`sort(using:)`](/documentation/Foundation/NSMutableArray/sort(using:)-4eh07))
- Comparing two objects directly (see [`compare(_:to:)`](/documentation/Foundation/NSSortDescriptor/compare(_:to:)))
- Specifying the order of objects that return from a Core Data fetch request (see <doc://com.apple.documentation/documentation/CoreData/NSFetchRequest/sortDescriptors>)

## Topics

### Creating a Sort Descriptor

[`+  sortDescriptorWithKey:ascending:`](/documentation/Foundation/NSSortDescriptor/sortDescriptorWithKey:ascending:)

Creates and returns a sort descriptor with the specified key path and ordering.

[`-  initWithKey:ascending:`](/documentation/Foundation/NSSortDescriptor/init(key:ascending:))

Creates a sort descriptor with a specified string key path and sort order.

[`+  sortDescriptorWithKey:ascending:selector:`](/documentation/Foundation/NSSortDescriptor/sortDescriptorWithKey:ascending:selector:)

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

[`-  initWithKey:ascending:selector:`](/documentation/Foundation/NSSortDescriptor/init(key:ascending:selector:))

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

[`init(keyPath:ascending:)`](/documentation/Foundation/NSSortDescriptor/init(keyPath:ascending:))

Creates a sort descriptor with a specified key path and ordering.

[`+  sortDescriptorWithKey:ascending:comparator:`](/documentation/Foundation/NSSortDescriptor/sortDescriptorWithKey:ascending:comparator:)

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

[`-  initWithKey:ascending:comparator:`](/documentation/Foundation/NSSortDescriptor/init(key:ascending:comparator:))

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

[`init(keyPath:ascending:comparator:)`](/documentation/Foundation/NSSortDescriptor/init(keyPath:ascending:comparator:))

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

[`-  initWithCoder:`](/documentation/Foundation/NSSortDescriptor/init(coder:))

Creates a sort descriptor by decoding from the coder you specify.

[`init(_:)`](/documentation/Foundation/NSSortDescriptor/init(_:)-7qf91)

Creates a sort descriptor using a sort descriptor you specify.

### Getting Information About a Sort Descriptor

[`ascending`](/documentation/Foundation/NSSortDescriptor/ascending)

A Boolean value that indicates whether the receiver specifies sorting in ascending order.

[`key`](/documentation/Foundation/NSSortDescriptor/key)

The key that specifies the property to compare during sorting.

[`keyPath`](/documentation/Foundation/NSSortDescriptor/keyPath)

The key path that specifies the property to compare during sorting.

[`selector`](/documentation/Foundation/NSSortDescriptor/selector)

The selector for comparing objects.

[`comparator`](/documentation/Foundation/NSSortDescriptor/comparator)

The comparator for the sort descriptor.

### Using Sort Descriptors

[`-  compareObject:toObject:`](/documentation/Foundation/NSSortDescriptor/compare(_:to:))

Returns a comparison result value that indicates the sort order of two objects.

[`reversedSortDescriptor`](/documentation/Foundation/NSSortDescriptor/reversedSortDescriptor)

Returns a sort descriptor that reverses the sort order.

[`-  allowEvaluation`](/documentation/Foundation/NSSortDescriptor/allowEvaluation())

Forces a securely decoded sort descriptor to allow evaluation.

## Relationships

### Conforms To

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

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

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

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

[`NSObjectProtocol`](/documentation/ObjectiveC/NSObjectProtocol)

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

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

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

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

### Inherits From

[`NSObject-swift.class`](/documentation/ObjectiveC/NSObject-swift.class)

---

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)