Created
October 9, 2015 15:50
-
-
Save dzenbot/f4f6becb73702d5395d1 to your computer and use it in GitHub Desktop.
Sort UIColors in a human way (snippet from http://stackoverflow.com/a/8585285)
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
NSArray *sorted = [[dict allValues] sortedArrayUsingComparator:^NSComparisonResult(UIColor* obj1, UIColor* obj2) { | |
float hue, saturation, brightness, alpha; | |
[obj1 getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha]; | |
float hue2, saturation2, brightness2, alpha2; | |
[obj2 getHue:&hue2 saturation:&saturation2 brightness:&brightness2 alpha:&alpha2]; | |
if (hue < hue2) | |
return NSOrderedAscending; | |
else if (hue > hue2) | |
return NSOrderedDescending; | |
if (saturation < saturation2) | |
return NSOrderedAscending; | |
else if (saturation > saturation2) | |
return NSOrderedDescending; | |
if (brightness < brightness2) | |
return NSOrderedAscending; | |
else if (brightness > brightness2) | |
return NSOrderedDescending; | |
return NSOrderedSame; | |
}]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment