Defines the signature for a block object used for comparison operations.
SDKs
- iOS 4.0+
- macOS 10.6+
- Mac Catalyst 13.0+
- tvOS 9.0+
- watchOS 2.0+
Framework
- Foundation
Declaration
typealias Comparator = (Any, Any) -> Comparison Result
Discussion
The arguments to the Block object are two objects to compare. The block returns an Comparison
value to denote the ordering of the two objects.
You use NSComparator
blocks in comparison operations such as NSArray
’s sorted
, 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;
}];