NSMutableArray has several sorting methods. Select one of them, execute the sort selector, block or function and compare the y values. Here is an example of using blocks:
NSComparator comparatorBlock = ^(UIView *obj1, UIView *obj2) { if (obj1.frame.origin.y > obj2.frame.origin.y) { return (NSComparisonResult)NSOrderedDescending; } if (obj1.frame.origin.y < obj2.frame.origin.y) { return (NSComparisonResult)NSOrderedAscending; } return (NSComparisonResult)NSOrderedSame; }; [array sortUsingComparator:comparatorBlock];
source share