Type Alias

Comparator

Defines the signature for a block object used for comparison operations.

Declaration

typealias Comparator = (Any, Any) -> ComparisonResult

Discussion

The arguments to the Block object are two objects to compare. The block returns an ComparisonResult value to denote the ordering of the two objects.

You use NSComparator blocks in comparison operations such as NSArray’s sortedArray(comparator:), for example:

NSArray *sortedArray = [array sortedArrayUsingComparator: ^(id obj1, id obj2) {
 
    if ([obj1 integerValue] > [obj2 integerValue]) {
        return (NSComparisonResult)NSOrderedDescending;
    }
 
    if ([obj1 integerValue] < [obj2 integerValue]) {
        return (NSComparisonResult)NSOrderedAscending;
    }
    return (NSComparisonResult)NSOrderedSame;
}];

See Also

Sorting

var sortedArrayHint: Data

Analyzes the array and returns a “hint” that speeds the sorting of the array when the hint is supplied to sortedArray(_:context:hint:).

func sortedArray((Any, Any, UnsafeMutableRawPointer?) -> Int, context: UnsafeMutableRawPointer?)

Returns a new array that lists the receiving array’s elements in ascending order as defined by the comparison function comparator.

func sortedArray((Any, Any, UnsafeMutableRawPointer?) -> Int, context: UnsafeMutableRawPointer?, hint: Data?)

Returns a new array that lists the receiving array’s elements in ascending order as defined by the comparison function comparator.

func sortedArray(using: [NSSortDescriptor])

Returns a copy of the receiving array sorted as specified by a given array of sort descriptors.

func sortedArray(using: Selector)

Returns an array that lists the receiving array’s elements in ascending order, as determined by the comparison method specified by a given selector.

func sortedArray(comparator: (Any, Any) -> ComparisonResult)

Returns an array that lists the receiving array’s elements in ascending order, as determined by the comparison method specified by a given NSComparator block.

func sortedArray(options: NSSortOptions = [], usingComparator: (Any, Any) -> ComparisonResult)

Returns an array that lists the receiving array’s elements in ascending order, as determined by the comparison method specified by a given NSComparator block.