PDA

View Full Version : How do I order an array by neither ASC or DESC ?


Antivirus
07-18-2007, 07:04 PM
I have an array of values such as follows: '4,8,8,7,4,7,8'

and I want to them order them in the following order: 7,4,8

so that the array orders like this: '7,7,4,4,8,8,8'

Is there a function (in php or mysql) which defines custom sort orders? I have been searching the php manual but can't seem to find anything

Farcaster
07-18-2007, 09:08 PM
You could use a CASE statement, like:

ORDER BY
(CASE WHEN field = '7' THEN 1
WHEN field = '4' THEN 2
WHEN field = '8' THEN 3 END) ASC

I may have mistook you, you mean to order the rows in a particular order?

El_Muerte
07-19-2007, 06:14 AM
<a href="http://www.php.net/usort" target="_blank">http://www.php.net/usort</a>

Antivirus
07-19-2007, 11:22 AM
Farcaster, I would have never thought of using CASE, but it works very nicely - thanks!

El Muerte, I am going to try it with usort as well and see how it goes. thanks!