Skip to content

Instantly share code, notes, and snippets.

@hartbit
Created April 19, 2011 14:03
Show Gist options
  • Save hartbit/927915 to your computer and use it in GitHub Desktop.
Save hartbit/927915 to your computer and use it in GitHub Desktop.
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