Re: Multi-Sort -- how to do this?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, 2009-06-17 at 17:11 -0400, tedd wrote:
> Hi gang:
> 
> Here's the problem. Let's say you have a collection of arrays, such as:
> 
> $a = array();
> $b = array();
> $c = array();
> $d = array();
> 
> And then you populate the arrays like so:
> 
> while(...)
>     {
>     $a[] = ...
>     $b[] = ...
>     $c[] = ...
>     $d[] = ...
>     }
> 
> Now, let's say you want to sort the $d array, but you also want the 
> arrays $a, $b, and $c to be arranged in the same resultant order as 
> $d.
> 
> For example, please follow this:
> 
> Before sort of $d:
> $a = [apple, banana, grape, orange]
> $b = [100, 2111, 198, 150]
> $c = [red, yellow, purple, orange]
> $d = [100, 300, 11, 50]
> 
> After sort of $d:
> $a = [grape, orange, apple, banana]
> $b = [198, 150, 100, 2111]
> $c = [purple, orange, red, yellow]
> $d = [11, 50, 100, 300]
> 
> Is there a slick way to do that?
> 
> Cheers,
> 
> tedd
> 
> -- 
> -------
> http://sperling.com  http://ancientstones.com  http://earthstones.com
> 
I'd probably go with some sort of custom bubble sorting function. Base
the sorting on your $d array, and then update the other arrays as
necessary. Should be OK if they all have the same index, like in your
example. If you were using keys, could you maybe join all the arrays
using some sort of serialisation, sort, then unserialise into the
separate arrays?


Thanks
Ash
www.ashleysheridan.co.uk


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux