In Part 2 we looked at how to order a single-dimension array using an array of indexes. In Part 1 we examined a very quick and dirty way to reorder an array. In this final part, we'll look at a more real-world application of reordering arrays - the ability to display Recordsets in random order!
Displaying a Recordset in Random Order
You may be questioning the usefulness of randomly ordering an array, and for good reason. At first glance, there appears to be just a few cases where randomly ordering an array may seem useful. Upon closer inspection, however, one realizes that he or she can use random array reordering to very efficiently display the contents of a Recordset in random order!
To do this, we must first get the contents of a Recordset into an array. Thankfully, ADO
provides this functionality with the
returns a two dimensional array... the first dimension contains the various columns while
the second dimension contains the various rows.
(To learn more about
GetRows check out: Using
GetRows to Speed Up Displaying Skinny Tables!)
Due to the fact that we need to reorder
a two-dimensional array, we will need to tweak the above redordering function
ReOrderArrayIndexed). The tweaked function is available below:
Before we call
ReOrderArray2D, we need to use the
method to convert a Recordset into an array. This is quite simple, and can be seen
with the following code:
Anytime after this code, we can randomly reorder the array
aRows using the
function we examined above (
ReOrderArray2D). (View a live demo!)
Personally, I find the above
code to be a very neat way to quickly and efficiently to display an array
in random order. There are other ways to randomly jumble up an array... be sure to
check out Returning Rows in Random Order for
information on alternative techniques!
In this article we examined a few techniques for randomly ordering the contents of an array. We also looked at how to use this "technique" for a very real-world, everyday situation - displaying the contents of a Recordset in random order.