Sorting List in Random Order

I

Idaho Word Man

I have a long list of items (about 500 items and growing) that currently is
sorted alphabetically. I want to change it so that the order is random. (I
don't want students to be able to guess the next item.) Is there an easy way
of doing this short of shuffling things by hand?

I'm in Office 2003 and XP Professional.

Thanks.

Fred
 
G

grammatim

Pushing the problem up a level, you could use a random number
generator to give you 500+ numbers, make a two-column table with the
numbers in one column and the items in the other, and sort the random
numbers.
 
D

dp

Could you copy to excel and use function =Right(a1,2), then sort on those 2
characters? I've just been playing with excel so thought of this Please
ignore my response if i'm way off base.
 
J

Jay Freedman

Idaho said:
I have a long list of items (about 500 items and growing) that
currently is sorted alphabetically. I want to change it so that the
order is random. (I don't want students to be able to guess the next
item.) Is there an easy way of doing this short of shuffling things
by hand?

I'm in Office 2003 and XP Professional.

Thanks.

Fred

You know there had to be a macro solution lurking somewhere. <g>

Sub RandomizeParas()
Dim oTbl As Table
Dim oCol As Column
Dim nRow As Long, randRow As Long
Dim maxRows As Long
Dim aNums() As Long
Dim tmp As Long
Dim srcDoc As Document
Dim destDoc As Document

Set srcDoc = ActiveDocument
Set destDoc = Documents.Add

' Copy the text into a new blanks document
' and turn it into a 1-column table.
With destDoc
.Range.InsertFile srcDoc.FullName
Set oTbl = .Range.ConvertToTable _
(Separator:=wdSeparateByParagraphs, _
NumColumns:=1, ApplyHeadingRows:=False)
End With
' no longer need the original to be open
srcDoc.Close SaveChanges:=wdDoNotSaveChanges

' Make an array with one unique number for each row
' of the table, and randomize the numbers' order
' by swapping each entry with some random entry.
Randomize ' different sequence of numbers each time
maxRows = oTbl.Rows.Count

ReDim aNums(maxRows - 1)
For nRow = 0 To maxRows - 1
aNums(nRow) = nRow
Next

For nRow = 0 To maxRows - 1
' pick a random entry...
randRow = CLng((maxRows - 1) * Rnd)
' this is the swap...
tmp = aNums(nRow)
aNums(nRow) = aNums(randRow)
aNums(randRow) = tmp
Next

' Add a new column to the right side of the table,
' fill it with the randomized numbers, sort the table
' on that column, then delete the new column and
' convert the rest back to (now randomized) text.
With oTbl
Set oCol = .Columns.Add
For nRow = 1 To .Rows.Count
oCol.Cells(nRow).Range.Text = CStr(aNums(nRow - 1))
Next
.Sort FieldNumber:=2, SortFieldType:=wdSortFieldNumeric, _
ExcludeHeader:=False
.Columns(2).Delete
.ConvertToText
End With
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
I

Idaho Word Man

Thanks to all who replied.

Yes, Jay, I knew there would be a macro solution "lurking somewhere," but
macros still scare me. (And they aren't always workable with our uber-secure
government computers.)

Grammatim and DP: I used kind of a combination of your two suggestions and
used a Word table to do the sorting. It worked well enough for my needs.

Thanks again.

Fred
 
G

grammatim

So where'd you get your random numbers from? Is that something Excel
will do for you?
 
I

Idaho Word Man

It was such a low-tech, jerry-rigged way of doing it that I almost hate to
admit how I did it. I really didn't need statistically "random" numbers -- I
just needed to scramble my list so it wasn't predictable. So when I learned
from your responses that there wasn't an easy randomize function, I put my
list into a table and added two columns to the left. Then I scrambled the
numbers 0 to 9 by hand and pasted them into the first column repeatedly. Then
I scrambled the numbers 0 to 17 by hand (I figured that an odd number like 17
would counteract the pattern in the first column) and pasted it into the
second column repeatedly. Then I sorted the table, deleted the columns of
numbers, and converted the table back to text. It's now scrambled
sufficiently for my needs.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top