Welcome to WebmasterWorld Guest from 107.22.109.65

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Trouble with sorting an array

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

Preferred Member

10+ Year Member

joined:Apr 29, 2003
posts:424
votes: 0


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?

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

Preferred Member

10+ Year Member

joined:Apr 29, 2003
posts:424
votes: 0


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)

Administrator

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

joined:July 31, 2003
posts:12547
votes: 2


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?
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members