Class

NSSortDescriptor

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

Declaration

@interface NSSortDescriptor : NSObject

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

+ sortDescriptorWithKey:ascending:

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

- initWithKey:ascending:

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

+ sortDescriptorWithKey:ascending:selector:

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

- initWithKey:ascending:selector:

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

+ sortDescriptorWithKey:ascending:comparator:

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

- initWithKey:ascending:comparator:

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

Getting Information About a Sort Descriptor

ascending

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

key

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

selector

The selector used when comparing objects.

comparator

The comparator for the sort descriptor.

Using Sort Descriptors

- compareObject:toObject:

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

reversedSortDescriptor

Returns a sort descriptor with the sort order reversed.

- allowEvaluation

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

Initializers

Relationships

Inherits From

See Also

Sorting

NSComparisonResult

Constants that indicate sort order.

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software