Re: Sorting problem
| From: | David Otton | Date: | Sat, 28 Jun 2003 00:41:09 +0000 |
| Subject: | Re: Sorting problem | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-153231@lists.php.net to get a copy of this message | ||
On Fri, 27 Jun 2003 17:06:50 -0600, you wrote:
>I need to sort an array and have the key 'follow' the value. I know I could
>splice a string, sort it, then split it again but what a pain. Is there a
>simpler way?
AFAIK, there's no one-liner answer here. the usort() family of functions
pass either the key or the value, but never both.
I think you'll have to write this up yourself... depending on your data, you
may find that ksort() will get it 95% in-place, and a quick custom bubble
sort will get you the rest of the way. Or you could just code up your own
quicksort.
Alternatively, if you tweaked your data so it was a numerically-ordered
array of arrays, eg
array(array('key','value'),array('key','value'),array('key','value'))
instead of
array('key'=>'value','key'=>'value','key'=>'value')
then usort() would become a useful solution.
BTW, what's the data source? If it's coming from a SQL query, you can sort
the data on the way in and solve the problem that way.