To read the article online, visit http://www.4GuysFromRolla.com/webtech/110800-1.2.shtml

Randomly Reordering an Array, Part 2

• In Part 1 we examined just one way to randomly reorder an array. In this part we'll look at alternative method!

Now, this is just one way of reordering an array. Another way (which I think looks uber-cool) is to create a separate array that contains the indexes of the first array. So, if our array to reorder had five elements (0 through 4), our index array would contain five elements too. Initially, this array would contain the following values:

Index Array (`aTmp`)
IndexValue
00
11
22
33
44

In our reordering function, we'll jumble up the values of the index array and then use these new values to determine the ordering of the array that we are reordering. I know this may sound a bit confusing, so hopefully this will clear things up a bit. Imagine that the array we wish to reorder contains the following indexes/values.

Array to Reorder
IndexValue
0Scott
1Bob
2Steve
3Frank
4Charles

Now, imagine that, after jumbling about our array of indexes, this array - `aTmp` - contains the following values:

Index Array (`aTmp`)
IndexValue
03
10
21
34
42

Then, our reordered array would have the third element listed first, then the zeroth element, then the first element, then the fourth, and finally the second. (The order of the values in the `aTmp` array):

Array to Reorder
IndexValue
0Frank
1Scott
2Bob
3Charles
4Steve

Hopefully that makes sense. Now, in our reordering function, we will create our indexer array, `aTmp`, and populate it with the available indexes (0 through 4 in the above example). Then, we will iterate through the values of the array we wish to reorder. In each iteration, we will randomly select a value from the `aTmp` array. Since a value in `aTmp` is an index, we'll use this information to know what value from the array to order to put where... We will then resize the `aTmp` array, removing the index we just plucked out. OK, OK, I know that all may sound very confusing, but hopefully an example will help:

 ```Function ReOrderArrayIndexed(ByVal aArray) Dim iCount iCount = UBound(aArray) 'Create a string of indexes Dim iLoop, strIndex, iUpper, iLower iLower = LBound(aArray) For iLoop = iLower to iCount strIndex = strIndex & CStr(iLoop) If iLoop < iCount then strIndex = strIndex & "," Next 'Choose a Random Index Randomize Timer Dim iRnd, aTmp, iTmpUpper, strNewIndex Redim aTmp(iCount) 'Iterate through, plucking out indexes from aTmp For iLoop = iLower to iCount iTmpUpper = iCount - iLoop 'Rebuild the aTmp array ReDim Preserve aTmp(iTmpUpper) aTmp = split(strIndex, ",") 'Choose a spot in the index array iRnd = Int(Rnd * (iTmpUpper + 1)) 'Build up the reordered array in string form strNewIndex = strNewIndex & aArray(aTmp(iRnd)) & "," 'Remove the chosen index to the end of the array and 'chop off the last element aTmp(iRnd) = aTmp(iTmpUpper) ReDim Preserve aTmp(iTmpUpper - 1) strIndex = join(aTmp, ",") Next 'At this point, strNewIndex contains a comma at the end, 'so we need to hack this off before building the array 'with split... strNewIndex = Left(strNewIndex, Len(strNewIndex) - 1) ReOrderArrayIndexed = split(strNewIndex, ",") End Function ```
[View the live demo!]

Obviously the above method is much more complex that the first example we looked at. Also, the above example has a limitation - since it uses the `split` and `join` functions it will only work with single-dimensional arrays. (For more information on the split and join functions, be sure to read: Parsing with `split` and `join`!)

In Part 3 we'll examine how to use array reordering to display Recordsets in random order!