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

Comments (5)  |  Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page




Adobe ColdFusion 8.0.1 Update - Helping Programmers To Be Signifanctly Less Girlie - Download ColdFusion 8 Update 8.0.1 Now.

Reader 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!

Posted by Seb Duggan on Sep 29, 2006 at 8:19 AM


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

Posted by Ben Nadel on Sep 29, 2006 at 9:38 AM


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

Posted by anonymouse on Jan 28, 2007 at 4:17 AM


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)!

Posted by Kay Smoljak on Jul 9, 2008 at 7:30 PM


@Kay,

Anytime :) Thanks for dropping by.

Posted by Ben Nadel on Jul 14, 2008 at 8:51 AM


Post Comment  |  Ask Ben


Home   |   Web Log   |   ColdFusion   |   Projects   |   Resume   |   Job Form   |   Search   |   Contact
Epicenter Consulting - Custom Software Solutions for Business Evolution HostMySite.com - The Leader In ColdFusion Hosting