Comparing two values and returning the greatest of the two

If you have two CGPoints, if one is above the other, then it has a lower y value than the other.

For example: take two points:

CGPoint point1 = CGPointMake(100.0, 120.0);
CGPoint point2 = CGPointMake(100.0, 80.0);

You could write a method:

- (NSComparisonResult)compareYValueOfPoint:(CGPoint)point1 toPoint:(CGPoint)point2
{
    CGFloat y1 = point1.y;
    CGFloat y2 = point2.y;

    if (y1 == y2) {
        return NSOrderedSame;
    } else if (y1 < y2) {
        return NSOrderedAscending;
    } else {
        return NSOrderedDescending;
    }
}

and call it with:

    NSComparisonResult result = [self compareYValueOfPoint:point1 toPoint:point2];

And depending on the result, use the relevant point for further actions.

Leave a Comment