Re: [PHP] Multi-Sort -- how to do this?
At 3:36 PM -0400 6/18/09, Robert Cummings wrote: tedd wrote: At 1:45 PM -0400 6/18/09, Robert Cummings wrote: Thanks to Rob and Daniel for also playing, better luck next time. :-) <- note smiley -- I do appreciate your time. Mine version retains key associations. It wasn't a complete waste of time. Cheers, Rob. Rob: Nothing you provide me is a complete waste of time -- see here: http://www.webbytedd.com//multisort-rob/ Many thanks for your code. I meant to have a smiley at the end of it. I don't mind doing it, even thought he solution existed for multisort, thinking through the process is invaluable :) Cheers, Rob. Rob: Understood -- but just as well -- I don't pass up on any tidbits you provide even if I don't fully understand them (i.e., your push/pull example). And no I don't need an explanation, that understanding will come in time. It akin to fully understanding OOP. If you understand it the first time your exposed to it, you're lucky. After a few more exposures, you finally get it and wonder what the confusion was about. It just takes time. Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Multi-Sort -- how to do this?
On Thu, 2009-06-18 at 13:04 -0400, tedd wrote: > > > Ash: > >> > >> You missed the point. I could use the built-in sort (i.e., sort() ) > >> and sort the $d array. However, I would like the indexes of the other > >> arrays to match the new sort. > >> > >> Cheers, > >> > > > tedd > > > > >I think I might need a for-instance here, as you lost me! > > > >Thanks > >Ash > > > Ash: > > You can sort any array by using the "built-in" php function sort() -- try it. > > After you try it, you'll find that the array is sorted but the order > of the values are not as they were before. In other words, the > index-value pairs are different. Here's an example: > > http://www.webbytedd.com//sort/ > > Before the sort, the value 'red' is located in the $a array under index 0 > > After the sort, the value 'red' is still in the $a array but under > index 2. Note, the values of the indexes have changed. > > My query was simply how to sort one array (order it) while ordering > the values of other arrays in the same manner. > > The multisort() function does that as seen here: > > http://www.webbytedd.com//multisort/ > > Cheers, > > tedd > > -- > --- > http://sperling.com http://ancientstones.com http://earthstones.com > I know about the sort() function, I just didn't fully understand the question. I guess that's what late nights can do to ya! Thanks Ash www.ashleysheridan.co.uk -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Multi-Sort -- how to do this?
tedd wrote: At 1:45 PM -0400 6/18/09, Robert Cummings wrote: Thanks to Rob and Daniel for also playing, better luck next time. :-) <- note smiley -- I do appreciate your time. Mine version retains key associations. It wasn't a complete waste of time. Cheers, Rob. Rob: Nothing you provide me is a complete waste of time -- see here: http://www.webbytedd.com//multisort-rob/ Many thanks for your code. I meant to have a smiley at the end of it. I don't mind doing it, even thought he solution existed for multisort, thinking through the process is invaluable :) Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Multi-Sort -- how to do this?
At 1:45 PM -0400 6/18/09, Robert Cummings wrote: Thanks to Rob and Daniel for also playing, better luck next time. :-) <- note smiley -- I do appreciate your time. Mine version retains key associations. It wasn't a complete waste of time. Cheers, Rob. Rob: Nothing you provide me is a complete waste of time -- see here: http://www.webbytedd.com//multisort-rob/ Many thanks for your code. Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Multi-Sort -- how to do this?
tedd wrote: At 10:11 AM +0100 6/18/09, Ford, Mike wrote: On 17 June 2009 22:12, tedd advised: -snip- > 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] array_multisort($d, $a, $b, $c) should do what you want, Cheers! Mike Mike: Slick indeed! That's exactly what I was looking for (see subject line -- I was close). Here's the working example: http://www.webbytedd.com//multisort/ Thanks to Rob and Daniel for also playing, better luck next time. :-) <- note smiley -- I do appreciate your time. Mine version retains key associations. It wasn't a complete waste of time. Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Multi-Sort -- how to do this?
> Ash: You missed the point. I could use the built-in sort (i.e., sort() ) and sort the $d array. However, I would like the indexes of the other arrays to match the new sort. Cheers, > tedd > I think I might need a for-instance here, as you lost me! Thanks Ash Ash: You can sort any array by using the "built-in" php function sort() -- try it. After you try it, you'll find that the array is sorted but the order of the values are not as they were before. In other words, the index-value pairs are different. Here's an example: http://www.webbytedd.com//sort/ Before the sort, the value 'red' is located in the $a array under index 0 After the sort, the value 'red' is still in the $a array but under index 2. Note, the values of the indexes have changed. My query was simply how to sort one array (order it) while ordering the values of other arrays in the same manner. The multisort() function does that as seen here: http://www.webbytedd.com//multisort/ Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Multi-Sort -- how to do this?
At 10:11 AM +0100 6/18/09, Ford, Mike wrote: On 17 June 2009 22:12, tedd advised: -snip- > 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] array_multisort($d, $a, $b, $c) should do what you want, Cheers! Mike Mike: Slick indeed! That's exactly what I was looking for (see subject line -- I was close). Here's the working example: http://www.webbytedd.com//multisort/ Thanks to Rob and Daniel for also playing, better luck next time. :-) <- note smiley -- I do appreciate your time. Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Multi-Sort -- how to do this?
On Thu, Jun 18, 2009 at 05:11, Ford, Mike wrote: > > array_multisort($d, $a, $b, $c) should do what you want, That'll actually hit it right on the head, too, Mike. My example from last night sorted and combined the arrays to be keyed and sortable by $d --- which, re-reading this morning, is not what Tedd wanted. He wants to be able to run a loop and echo out each individual array in place. In which case, array_multisort() is proper. Show-off. -- daniel.br...@parasane.net || danbr...@php.net http://www.parasane.net/ || http://www.pilotpig.net/ 50% Off All Shared Hosting Plans at PilotPig: Use Coupon DOW1 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Multi-Sort -- how to do this?
From: Ashley Sheridan > On Wed, 2009-06-17 at 19:27 -0400, tedd wrote: >> At 10:54 PM +0100 6/17/09, Ashley Sheridan wrote: >> >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? >> > >> Ash: >> >> You missed the point. I could use the built-in sort (i.e., sort() ) >> and sort the $d array. However, I would like the indexes of the other >> arrays to match the new sort. >> > > I think I might need a for-instance here, as you lost me! > He is emulating a four column table using four arrays. He wants to sort the whole table on one of the columns. Bob McConnell -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Multi-Sort -- how to do this?
From: Ashley Sheridan > On Wed, 2009-06-17 at 19:27 -0400, tedd wrote: >> At 10:54 PM +0100 6/17/09, Ashley Sheridan wrote: >> >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? >> > >> Ash: >> >> You missed the point. I could use the built-in sort (i.e., sort() ) >> and sort the $d array. However, I would like the indexes of the other >> arrays to match the new sort. >> > > I think I might need a for-instance here, as you lost me! > He is emulating a four column table using four arrays. He wants to sort the table on one of the columns. Bob McConnell -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Multi-Sort -- how to do this?
Ford, Mike wrote: On 17 June 2009 22:12, tedd advised: 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] array_multisort($d, $a, $b, $c) should do what you want That's even slicker than tedd_sort(). Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Multi-Sort -- how to do this?
On 17 June 2009 22:12, tedd advised: > 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] array_multisort($d, $a, $b, $c) should do what you want, Cheers! Mike -- Mike Ford, Electronic Information Developer, C507, Leeds Metropolitan University, Civic Quarter Campus, Woodhouse Lane, LEEDS, LS1 3HE, United Kingdom Email: m.f...@leedsmet.ac.uk Tel: +44 113 812 4730 To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Multi-Sort -- how to do this?
On Wed, Jun 17, 2009 at 10:31:18PM -0400, Robert Cummings wrote: > > > function tedd_sort( &$arrays ) > { > $master = null; > $followers = array(); > > $first = true; > foreach( array_keys( $arrays ) as $key ) > { > if( $first ) > { > $first = false; > $master = &$arrays[$key]; > } > else > { > $followers[] = &$arrays[$key]; > } > } > > > asort( $master ); > foreach( array_keys( $master ) as $mkey ) > { > foreach( array_keys( $followers ) as $fkey ) > { > $value = &$followers[$fkey][$mkey]; > unset( $followers[$fkey][$mkey] ); > $followers[$fkey][$mkey] = &$value; > } > } > } > > $a = array( 'apple', 'banana', 'grape', 'orange' ); > $b = array( 100, 2111, 198, 150 ); > $c = array( 'red', 'yellow', 'purple', 'orange' ); > $d = array( 100, 300, 11, 50 ); > > $arrays = array( &$d, &$a, &$b, &$c ); > tedd_sort( $arrays ); > print_r( $a ); > print_r( $b ); > print_r( $c ); > print_r( $d ); > > ?> Wow, tedd has a sort named after him! I'm sooo jealous. Paul -- Paul M. Foster -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Multi-Sort -- how to do this?
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? Yes... If func_get_args() or func_get_arg() had supported retrieving a reference to the argument, then we could have saved having to pass the arrays via a combined array. I chose to make $d the first array since in your example result set it is the one sorted and thus the rest follow as you would expect. Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Multi-Sort -- how to do this?
On Wed, Jun 17, 2009 at 17:11, tedd wrote: > Hi gang: > [snip!] > > Is there a slick way to do that? Hacked together in the two minutes before I go to bed, so don't complain about its inelegance. ;-P -- daniel.br...@parasane.net || danbr...@php.net http://www.parasane.net/ || http://www.pilotpig.net/ 50% Off All Shared Hosting Plans at PilotPig: Use Coupon DOW1 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Multi-Sort -- how to do this?
On Wed, 2009-06-17 at 19:27 -0400, tedd wrote: > At 10:54 PM +0100 6/17/09, Ashley Sheridan wrote: > >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 > > > Ash: > > You missed the point. I could use the built-in sort (i.e., sort() ) > and sort the $d array. However, I would like the indexes of the other > arrays to match the new sort. > > Cheers, > > tedd > > -- > --- > http://sperling.com http://ancientstones.com http://earthstones.com > I think I might need a for-instance here, as you lost me! Thanks Ash www.ashleysheridan.co.uk -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Multi-Sort -- how to do this?
At 10:54 PM +0100 6/17/09, Ashley Sheridan wrote: 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 Ash: You missed the point. I could use the built-in sort (i.e., sort() ) and sort the $d array. However, I would like the indexes of the other arrays to match the new sort. Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Multi-Sort -- how to do this?
On Wed, 2009-06-17 at 22:54 +0100, Douglas Temple wrote: > ...Sticking my neck out playing with the big boys (and girls) now... > > I assume you've looked at array_multisort() for this, I would think you > could have multiple arrays being sorted with it (I've only ever used it for > 2-array sorts). The other option would be to pop all those arrays into a > single array and use array_multisort()'s ability to sort > arrays-within-arrays. > > Otherwise Ashley's solution is another possibility; using the keys in the > sorted $d to dictate where you could place the elements in the other arrays. > I'd say you could do a nifty loop system which says sequentially read the > indices in $d and then make a new temp array that is $a, $b or $c (depending > on which you are sorting) in the sorted order then just name the temp array > as the original (again, I'd say array_multisort() is fancier than this crude > but elegant system). > > Finally, why not use quicksort or mergesort for the sorting? Bubblesort is > inefficient for large lists... I only mention using a bubblesort as it is the only sorting algorithm i know! Thanks Ash www.ashleysheridan.co.uk -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Multi-Sort -- how to do this?
...Sticking my neck out playing with the big boys (and girls) now... I assume you've looked at array_multisort() for this, I would think you could have multiple arrays being sorted with it (I've only ever used it for 2-array sorts). The other option would be to pop all those arrays into a single array and use array_multisort()'s ability to sort arrays-within-arrays. Otherwise Ashley's solution is another possibility; using the keys in the sorted $d to dictate where you could place the elements in the other arrays. I'd say you could do a nifty loop system which says sequentially read the indices in $d and then make a new temp array that is $a, $b or $c (depending on which you are sorting) in the sorted order then just name the temp array as the original (again, I'd say array_multisort() is fancier than this crude but elegant system). Finally, why not use quicksort or mergesort for the sorting? Bubblesort is inefficient for large lists...
Re: [PHP] Multi-Sort -- how to do this?
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