Class

NSSortDescriptor

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

Overview

You construct instances of NSSortDescriptor by specifying the key path of the property to be compared 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(_:) and localizedCaseInsensitiveCompare(_:). Sorting raises an exception if the objects to be sorted do not respond to the sort descriptor’s comparison selector.

You can use sort descriptors to, for example:

Topics

Initializing a Sort Descriptor

init(key: String?, ascending: Bool)

Initializes a sort descriptor a given key path and sort order.

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

Initializes a sort descriptor with a given key path, ordering, and comparison selector.

init(key: String?, ascending: Bool, comparator: Comparator)

Initializes a sort descriptor with a given key path and ordering, and a comparator block.

Getting Information About a Sort Descriptor

var ascending: Bool

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

var key: String?

The key that specifies the property to be compared during sorting.

var keyPath: AnyKeyPath?

The key path that specifies the property to be compared during sorting.

var selector: Selector?

The selector used when comparing objects.

var comparator: Comparator

The comparator for the sort descriptor.

Using Sort Descriptors

func compare(Any, to: Any)

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

var reversedSortDescriptor: Any

Returns a sort descriptor with the sort order reversed.

func allowEvaluation()

Forces a sort descriptor that was securely decoded to allow evaluation.

Relationships

Inherits From

See Also

Sorting

enum ComparisonResult

Constants that indicate sort order.