1

So I have on my page this line of code:

<cfoutput><li><a href="#ADDDestination#"><img src="#ADDSlide#" /></a></li>
<li><a href="#TAPDestination#"><img src="#TAPSlide#" /></a></li>
<li><a href="#ATHDestination#"><img src="#ATHSlide#" /></a></li>
<li><a href="#STADestination#"><img src="#STASlide#" /></a></li></cfoutput>

These items are generated randomly using an array. How could I further randomly order this listing of 4 tiles?

  • From where does the array come? – Dan Bracuk Sep 11 '15 at 16:05
  • Might want to search the archives first. There are several threads with suggestions on how to [randomize with arrays](http://stackoverflow.com/questions/7997810/using-coldfusion-how-do-i-display-the-elements-of-an-array-in-random-order/8002517#8002517) and also [with queries](http://stackoverflow.com/questions/4016505/help-getting-or-displaying-random-records-in-coldfusion-from-a-mysql-query). – Leigh Sep 11 '15 at 18:44

1 Answers1

4

You can use the shuffle method of a java.util.Collections to do this:

  <cfscript>
  items = [
      {"id":1, "key":"a"},
      {"id":2, "key":"b"},
      {"id":3, "key":"c"}
  ];

  Collection = CreateObject("java", "java.util.Collections");

  Collection.Shuffle(items);

  writeDump(items);
  </cfscript>

Each time you run it you'll get the items in a different order.

Hat tip to Mark Mandel who introduced it to me.

John Whish
  • 3,016
  • 17
  • 21