Randomly Sorting An Array In ColdFusion

Posted September 19, 2006 at 2:23 PM

Tags: ColdFusion

I touched upon this a bit when I was talking about randomly sorting a list in ColdFusion. Actually, one of the solutions to that problem was to create, as an interim object, a randomly sorted array. I figured as an example, I would just break it out here to see how it works.

To start with, as always, let's build an array:

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

  • <!--- Set the number of records. --->
  • <cfset intRecordCount = 4 />
  •  
  • <!--- Create an array. --->
  • <cfset arrGirls = ArrayNew( 1 ) />
  •  
  • <!--- Resize the array to be our test number. --->
  • <cfset ArrayResize( arrGirls, intRecordCount ) />
  •  
  • <!--- Now, we need to populate the array with data. --->
  • <cfloop index="intI" from="1" to="#intRecordCount#" step="1">
  •  
  • <!--- Create a struct for the girl data. --->
  • <cfset arrGirls[ intI ] = StructNew() />
  •  
  • <!--- Set the ID to be the index. --->
  • <cfset arrGirls[ intI ].ID = intI />
  •  
  • <!--- Now, randomly pick a girl to create. --->
  • <cfswitch expression="#RandRange( 1, 3 )#">
  • <cfcase value="1">
  •  
  • <cfset arrGirls[ intI ].Name = "Anna" />
  • <cfset arrGirls[ intI ].Hair = "Dirty Blonde" />
  •  
  • </cfcase>
  • <cfcase value="2">
  •  
  • <cfset arrGirls[ intI ].Name = "Alex" />
  • <cfset arrGirls[ intI ].Hair = "Blonde" />
  •  
  • </cfcase>
  • <cfcase value="3">
  •  
  • <cfset arrGirls[ intI ].Name = "Jo" />
  • <cfset arrGirls[ intI ].Hair = "Brunette" />
  •  
  • </cfcase>
  • </cfswitch>
  •  
  • </cfloop>

We should now have an array that is 4 items long:


 
 
 

 
CFDump 1 : Randomly Sorting An Array In ColdFusion  
 
 
 

I am keeping short for the demo. Now, we want to randomize the order of it. To do this, we are going to loop over the array N times and swap the item at the current index with the a random item in the array. I am not sure how *random* this is. I am sure that are mathematical proofs for determining how many times you should run across the array, but I am not gonna get into it. For now, this seems to work fine:

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

  • <!---
  • Loop over the array N times. In this case, I am
  • going to be looping twice.
  • --->
  • <cfloop index="intN" from="1" to="2" step="1">
  •  
  • <!--- Loop over the array. --->
  • <cfloop index="intI" from="1" to="#ArrayLen( arrGirls )#" step="1">
  •  
  • <!---
  • Swap the current item with a randomly chosen item.
  • We are getting the random index via RandRange().
  • --->
  • <cfset ArraySwap(
  • arrGirls,
  • intI,
  • RandRange( 1, ArrayLen( arrGirls ) )
  • ) />
  •  
  • </cfloop>
  •  
  • </cfloop>

Our array should now be randomly sorted:


 
 
 

 
CFDump 2 : Randomly Sorting An Array In ColdFusion  
 
 
 

I am not sure about speed testing because I don't really have anything to compare it to. The time it takes to process should increase linearly with the size of the array.

Download Code Snippet ZIP File

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




Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

Sep 19, 2006 at 10:02 PM // reply »
20 Comments

I think you'll hate me for this... but there is an easy java way to do this.

(this is off the top of my head, but it should work)

<cfscript>
Collections = createObject("java", "java.util.Collections");

Collections.shuffle(yourArray);
</cfscript>

There you go, the array is shuffled.

(I didn't test this btw, but I can't see why it wouldn't work)


Sep 20, 2006 at 7:41 AM // reply »
6,516 Comments

Mark,

Firstly, I am NEVER gonna hate learning how to do things an easier, simpler way.

Secondly, I am stuck without network access at the moment and can't test this stuff. But, from the documentation on collections, this seems totally valid. I am starting to like this Collections class.

I will test as soon as my boss gets here and give me back my network access :(


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 21, 2009 at 1:13 PM
My First ColdFusion Builder Extension - Encrypting And Decrypting CFM / CFC Files
@Ben, Because I am pedantic, I just want to make sure that everyone knows there is absolutely no encryption going on. There is only encoding and obfuscation. The cfencode tool only obfuscates your C ... read »
Nov 21, 2009 at 12:28 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jody I can't seem to get your code sample to work. If you are still having problems, try this code out and see if it gets you what you wanted. <!--- Comma delimited list with various duplicates ... read »
Nov 21, 2009 at 11:03 AM
Groovy Operator Overloading Does Not Work In The ColdFusion Context
Hi Ben, Thanks for this informative post. Now I am reading ur old posts too ... read »
Nov 21, 2009 at 10:56 AM
HostMySite.com Has The Best ColdFusion Hosting
@Mehul, Yes very nice people, however several downtimes per day which was not acceptable. Hence we had to move out. I am glad you are having good luck with them so far. ... read »
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 »