House of Fusion
Home of the ColdFusion Community

Search cf-talk

December 02, 2008

<<   <   Today   >   >>
Su Mo Tu We Th Fr Sa
   1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31       

Search over 2,500 ColdFusion resources here  >>>      
Home /  Groups /  ColdFusion Talk (CF-Talk)

ColdFusion 8: Reversing arrays?

  << Previous Post |  RSS |  Sort Oldest First |  Sort Latest First |  Subscribe to this Group Next >> 
Top  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Andy Matthews
05/08/2008 11:34 AM

It used to be possible to reverse an array, at least in CF6 and CF7 using the Reverse method. It appears however that Adobe has removed this functionality in CF8. Is there any single tag/function which picks up this functionality from the Reverse method? Andy Matthews

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Barney Boisvert
05/08/2008 12:34 PM

a = [1, 2, 3, 4, 5]; createObject("java", "java.util.Collections").reverse(a); a.toString(); // [5, 4, 3, 2, 1] Also of interest is Collections.sort(), which will sort an array using natural ordering.  You'll get errors if the contains types aren't all mutually naturally orderable (all implement Comparable<T>), of course. cheers, barneyb On Thu, May 8, 2008 at 8:22 AM, Andy Matthews <andymatthews@comcast.net> wrote: > It used to be possible to reverse an array, at least in CF6 and CF7 using the Reverse method. It appears however that Adobe has removed this functionality in CF8. Is there any single tag/function which picks up this functionality from the Reverse method? > > >  Andy Matthews

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Dominic Watson
05/08/2008 12:43 PM

> It used to be possible to reverse an array, at least in CF6 and CF7 using the Reverse method Reverse() is for reversing a string: http://livedocs.adobe.com/coldfusion/7/htmldocs/wwhelp/wwhimpl/common/html/wwhelp.htm?context=ColdFusion_Documentation&file=00000616.htm Here's a little udf for reversing an array using the Reverse() method: <cfscript>     function ReverseArray(arr){         return ListToArray(Reverse(ArrayToList(arguments.arr)));     } </cfscript> Perhaps there's a better way, i.e. using the java.utils.collection reverse method that Barney showed, but this seems straightforward to me. Dominic -- Blog it up: http://fusion.dominicwatson.co.uk

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Adrian Lynch
05/08/2008 12:58 PM

Not exactly what you'd want :OD <cfscript>     function ReverseArray(arr){         return ListToArray(Reverse(ArrayToList(arguments.arr)));     } </cfscript> <cfset names = ["Bill","Ben","Bob","Barney"]> <cfdump var="#ReverseArray(names)#"> > It used to be possible to reverse an array, at least in CF6 and CF7 using the Reverse method Reverse() is for reversing a string: http://livedocs.adobe.com/coldfusion/7/htmldocs/wwhelp/wwhimpl/common/html/w whelp.htm?context=ColdFusion_Documentation&file=00000616.htm Here's a little udf for reversing an array using the Reverse() method: <cfscript>     function ReverseArray(arr){         return ListToArray(Reverse(ArrayToList(arguments.arr)));     } </cfscript> Perhaps there's a better way, i.e. using the java.utils.collection reverse method that Barney showed, but this seems straightforward to me. Dominic -- Blog it up: http://fusion.dominicwatson.co.uk

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Barney Boisvert
05/08/2008 01:01 PM

I'm pretty sure that'll reverse the whole string, not the list elements, so you if you started with ["ab, "cd", "ef"], you'd end up with ["fe", "dc", "ba"], rather than the correct ["ef", "cd", "ab"]. I don't know if it would work correctly with dates either, since they'd be converted to strings, but wouldn't be converted back to dates.  The Collections.sort method, on the other hand, doesn't touch the objects, so neither problem would exist. cheers, barneyb On Thu, May 8, 2008 at 9:42 AM, Dominic Watson <watson.dominic@googlemail.com> wrote: ----- Excess quoted text cut - see Original Post for more -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Dominic Watson
05/08/2008 01:04 PM

>I'm pretty sure that'll reverse the whole string, not the list >elements, so you if you started with ["ab, "cd", "ef"], you'd end up >with ["fe", "dc", "ba"], rather than the correct ["ef", "cd", "ab"]. >I don't know if it would work correctly with dates either, since >they'd be converted to strings, but wouldn't be converted back to >dates.  The Collections.sort method, on the other hand, doesn't touch >the objects, so neither problem would exist. ;) yeh, didn't think - just typed. Not seen that java.utils.Collections object before - damn useful :) Dominic -- Blog it up: http://fusion.dominicwatson.co.uk

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Andy Matthews
05/08/2008 01:31 PM

Thanks Barney...I was actually looking through the Java methods but couldn't find that. Exactly what I'm looking for. a = [1, 2, 3, 4, 5]; createObject("java", "java.util.Collections").reverse(a); a.toString(); // [5, 4, 3, 2, 1] Also of interest is Collections.sort(), which will sort an array using natural ordering.  You'll get errors if the contains types aren't all mutually naturally orderable (all implement Comparable<T>), of course. cheers, barneyb On Thu, May 8, 2008 at 8:22 AM, Andy Matthews <andymatthews@comcast.net> wrote: > It used to be possible to reverse an array, at least in CF6 and CF7 using the Reverse method. It appears however that Adobe has removed this functionality in CF8. Is there any single tag/function which picks up this functionality from the Reverse method? > > >  Andy Matthews > >  

Top  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Dave Watts
05/08/2008 01:00 PM

> Here's a little udf for reversing an array using the Reverse() method: ----- Excess quoted text cut - see Original Post for more ----- I don't think that'll work. If you provide an array with positions containing more than one character, the characters within each position will also be reversed. Dave Watts, CTO, Fig Leaf Software http://www.figleaf.com/ Fig Leaf Training: Adobe/Google/Paperthin Certified Partners http://training.figleaf.com/ WebManiacs 2008: the ultimate conference for CF/Flex/AIR developers! http://www.webmaniacsconference.com/

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Dominic Watson
05/08/2008 01:03 PM

>I don't think that'll work. If you provide an array with positions > containing more than one character, the characters within each position will > also be reversed. ;) yup me = dumbass, just looked at the java.utils.Collections object - perfect for the job. http://publib.boulder.ibm.com/infocenter/wsadhelp/v5r1m2/index.jsp?topic=/com.sun.api.doc/java/util/Collections.html My post was more pointing the fact that Reverse() was never for reversing arrays (plus tacking on a stoopid pointless input) Dominic -- Blog it up: http://fusion.dominicwatson.co.uk


<< Previous Thread Today's Threads Next Thread >>

Mailing Lists