|
|
Home /
Groups /
ColdFusion Talk (CF-Talk)
ColdFusion 8: Reversing arrays?
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 05/08/08 11:34 A a = [1, 2, 3, 4, 5];Barney Boisvert 05/08/08 12:34 P > It used to be possible to reverse an array, at least in CF6 and CF7 using the Reverse methodDominic Watson 05/08/08 12:43 P Not exactly what you'd want :ODAdrian Lynch 05/08/08 12:58 P I'm pretty sure that'll reverse the whole string, not the listBarney Boisvert 05/08/08 01:01 P >I'm pretty sure that'll reverse the whole string, not the listDominic Watson 05/08/08 01:04 P Thanks Barney...I was actually looking through the Java methods but couldn'tAndy Matthews 05/08/08 01:31 P > Here's a little udf for reversing an array using the Reverse() method:Dave Watts 05/08/08 01:00 P >I don't think that'll work. If you provide an array with positionsDominic Watson 05/08/08 01:03 P 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 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 > 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 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 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 ----- >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 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 > > > 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/ >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
|
Mailing Lists
|
Latest Fusion Authority Articles
|
||||||