Created
April 19, 2011 14:03
-
-
Save hartbit/927915 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static NSComparator upComparator = ^NSComparisonResult(id obj1, id obj2) { | |
if ([obj1 position].y < [obj2 position].y) { | |
return NSOrderedAscending; | |
} else if ([obj1 position].y > [obj2 position].y) { | |
return NSOrderedDescending; | |
} else { | |
return NSOrderedSame; | |
} | |
}; | |
static NSComparator rightComparator = ^NSComparisonResult(id obj1, id obj2) { | |
if ([obj1 position].x > [obj2 position].x) { | |
return NSOrderedAscending; | |
} else if ([obj1 position].x < [obj2 position].x) { | |
return NSOrderedDescending; | |
} else { | |
return NSOrderedSame; | |
} | |
}; | |
static NSComparator downComparator = ^NSComparisonResult(id obj1, id obj2) { | |
if ([obj1 position].y > [obj2 position].y) { | |
return NSOrderedAscending; | |
} else if ([obj1 position].y < [obj2 position].y) { | |
return NSOrderedDescending; | |
} else { | |
return NSOrderedSame; | |
} | |
}; | |
static NSComparator leftComparator = ^NSComparisonResult(id obj1, id obj2) { | |
if ([obj1 position].x < [obj2 position].x) { | |
return NSOrderedAscending; | |
} else if ([obj1 position].x > [obj2 position].x) { | |
return NSOrderedDescending; | |
} else { | |
return NSOrderedSame; | |
} | |
}; | |
switch ([self gravity]) | |
{ | |
case DirectionUp: [[self cubes] sortUsingComparator:upComparator]; break; | |
case DirectionRight: [[self cubes] sortUsingComparator:rightComparator]; break; | |
case DirectionDown: [[self cubes] sortUsingComparator:downComparator]; break; | |
case DirectionLeft: [[self cubes] sortUsingComparator:leftComparator]; break; | |
default: HDFail(@"Invalid direction", HDFailureLevelFatal); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment