Comparator
Defines the signature for a block object used for comparison operations.
Declaration
typealias Comparator = (Any, Any) -> ComparisonResultDiscussion
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;
}];