homepage Welcome to WebmasterWorld Guest from 184.73.87.85
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Trouble with sorting an array
Sarah Atkinson




msg:4052290
 7:52 pm on Dec 31, 2009 (gmt 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?

 

Sarah Atkinson




msg:4052318
 8:18 pm on Dec 31, 2009 (gmt 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;

}

coopster




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

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?

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved