Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Trouble with sorting an array


Sarah Atkinson

7:52 pm on Dec 31, 2009 (gmt 0)

10+ Year Member

I have an array of hex numbers like : #FFF, #c49a6c, #f3f3f3, #4f0e04 ect.
I would like to have the array sorted so that all the grey ones are first then red tones then greens then blues.

anyone have a good sort function for this?

Sarah Atkinson

8:18 pm on Dec 31, 2009 (gmt 0)

10+ Year Member

I made this up... it doesn't work great but it is better then nothing. the problem being the hex colors are based on RGB the with the R being the first 2 char G next and B the last 2.

function cmp($a, $b) {
//if equal
if ($a == $b) {
return 0;
//remove the '#' and convert the hex number to another number to a decimal
return (hexdec(rtrim($a,'#')) < hexdec(rtrim($b,'#'))) ? -1 : 1;



9:02 pm on Jan 1, 2010 (gmt 0)

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

I'm not certain on the sorting part ... not that familiar with the color codes. However I am familiar enough to know that the # is usually on the left, no? In that case, won't you want to ltrim() that value?

Featured Threads

Hot Threads This Week

Hot Threads This Month