Randomly Sort A ColdFusion Array (Updated) - Thanks Mark Mandel

Posted September 20, 2006 at 1:48 PM

Tags: ColdFusion

I had recently posted a ColdFusion solution to randomly sorting an array. Mark Mandel graciously pointed out that in Java, there is already a way to do this utilizing the java.util.Collections class. The java.util.Collections class is comprised solely of static methods that manipulate collections. It just so happens that it has a method: Shuffle().

Implementing it in ColdFusion could not be easier:

 Launch code in new window » Download code as text file »

  • <!--- Create an array. --->
  • <cfset arrGirls = ListToArray(
  • "Sarah,Ashley,Anna,Libby"
  • ) />
  •  
  • <!--- Shuffle it (ie. randomly sort it). --->
  • <cfset CreateObject(
  • "java",
  • "java.util.Collections"
  • ).Shuffle(
  • arrGirls
  • ) />

The code is compact so it may be hard to read. First, we are creating an instance of the Collections class:

 Launch code in new window » Download code as text file »

  • <cfset CreateObject( "java", "java.util.Collections" ) />

This class has NO constructors, which is why we are not calling any Init() methods. Once we have the instance, we then call the static method Shuffle() and pass in the array, arrGirls. This method does not return a value. It is updating the array by reference, not value.

This should accept anything that implements the list interface, java.util.List.

How easy is that? Thanks a bunch Mark Mandel! Man, I really need to learn more about the Java 2 class libraries. There is so much stuff in there that is SOOO awesome for ColdFusion.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page





Reader Comments

Sep 29, 2006 at 8:19 AM // reply »
15 Comments

Wow, that's so much simpler than my method of getting a random selection of sidebar buttons from a SQL query (which basically entailed getting the first random number, getting the second and checking it wasn't the same as the first, getting the third etc...).

Not what you'd call efficient code, but I couldn't see an alternative!


Sep 29, 2006 at 9:38 AM // reply »
6,516 Comments

What can I say, Mark Mandel is the man :)


Jan 28, 2007 at 4:17 AM // reply »
1 Comments

THANKS for this... I've recently been dabbling in what I can do with utilizing the base java libraries and this is just slick!


Jul 9, 2008 at 7:30 PM // reply »
3 Comments

Heh, awesome. I spend five minutes refamiliarising myself with CF's built in randomizing functions in preparation for thinking through a solution to randomizing an array. Then on a whim I google it and BAM, first result, a solution in one line of code. I love CF - thanks Ben (and Mark)!


Jul 14, 2008 at 8:51 AM // reply »
6,516 Comments

@Kay,

Anytime :) Thanks for dropping by.


Nov 24, 2008 at 9:20 PM // reply »
1 Comments

Many thanks! Love your site!


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 20, 2009 at 11:32 PM
Five Months Without Hungarian Notation And I'm Loving It
I've used headless camel case for years for not only ColdFusion variables, but also SQL tables and fields... pretty much everything involving code. I also subscribe to the "don't abbreviate and clea ... read »
Nov 20, 2009 at 11:00 PM
Five Months Without Hungarian Notation And I'm Loving It
@Marcel, Yeah, I always err on the side of longer but more readable variable names. As for the camel casing of CF methods and the headless camel casing of custom items, I get around this by always ... read »
Nov 20, 2009 at 10:56 PM
Five Months Without Hungarian Notation And I'm Loving It
I use the following and love it: my.namespace.MyComponents.functionMethodsOrUDF() CONSTANT_VALUES_OR_PROPERTIES One thing I always try is to CamelCaseBuiltInColdFusionFunctions() so others can tell ... read »
Nov 20, 2009 at 5:38 PM
Learning ColdFusion 8: CFImage Part I - Reading And Writing Images
Hi Ben, Great article. I've been looking around to see if ColdFusion image engine can programatically create the following "wrap around" effect: http://www.creativepro.com/article/photoshop-s-she ... read »
Nov 20, 2009 at 5:35 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Dave: I talked to Gert he suggested: <cfhttp method="get" url="http://{some cf website}" result="stuff" addtoken="yes" /> Note the addition of cfhttp attribute addtoken. That should persist y ... read »
Nov 20, 2009 at 5:23 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Todd, Ahh, gotcha, yeah that makes sense. ... read »
Nov 20, 2009 at 5:17 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
Ben, sorry if I didn't make this clear. You can make it work like that if you want, just put <cfset session.foo = 1> (and <cfset application.foo = 1>) in your OnRequestStart() and it reve ... read »