' Return a sorted list of the words in this file.
Private Function GetFileWords(ByVal file_name As String) As _
String
Dim fnum As Integer
Dim file_contents As String
fnum = FreeFile
Open file_name For Input As #fnum
file_contents = Input$(LOF(fnum), #fnum)
Close #fnum
GetFileWords = GetWords(file_contents)
End Function
' Return a sorted list of the string's words.
Private Function GetWords(ByVal file_contents As String) As _
String
Dim i As Integer
Dim ch As String
Dim word_array() As String
Dim word_col As Collection
Dim word As String
Dim result As String
' Replace separator characters with spaces.
For i = 1 To Len(file_contents)
' See if this character is a letter or number.
ch = Mid$(file_contents, i, 1)
If Not ( _
(ch >= "A" And ch <= "Z") Or _
(ch >= "a" And ch <= "z") Or _
(ch >= "0" And ch <= "9") _
) Then
' Not a letter. Replace with space.
Mid$(file_contents, i, 1) = " "
End If
Next i
file_contents = LCase$(file_contents)
' Split the words.
word_array = Split(file_contents)
' Add the words to the word collection.
Set word_col = New Collection
On Error Resume Next
For i = LBound(word_array) To UBound(word_array)
word = word_array(i)
If Len(word) > 0 Then word_col.Add word, word
Next i
On Error GoTo 0
' Convert the collection into an array.
ReDim word_array(1 To word_col.Count)
For i = 1 To word_col.Count
word_array(i) = word_col(i)
Next i
' Sort the array.
Quicksort word_array, 1, word_col.Count
' Generate the result string.
result = ""
For i = 1 To word_col.Count
result = result & vbCrLf & word_array(i)
Next i
GetWords = Mid$(result, Len(vbCrLf) + 1)
End Function
' Use Quicksort to sort a list of strings.
'
' This code is from the book "Ready-to-Run
' Visual Basic Algorithms" by Rod Stephens.
' http://www.vb-helper.com/vba.htm
Private Sub Quicksort(list() As String, ByVal min As Long, _
ByVal max As Long)
Dim mid_value As String
Dim hi As Long
Dim lo As Long
Dim i As Long
' If there is 0 or 1 item in the list,
' this sublist is sorted.
If min >= max Then Exit Sub
' Pick a dividing value.
i = Int((max - min + 1) * Rnd + min)
mid_value = list(i)
' Swap the dividing value to the front.
list(i) = list(min)
lo = min
hi = max
Do
' Look down from hi for a value < mid_value.
Do While list(hi) >= mid_value
hi = hi - 1
If hi <= lo Then Exit Do
Loop
If hi <= lo Then
list(lo) = mid_value
Exit Do
End If
' Swap the lo and hi values.
list(lo) = list(hi)
' Look up from lo for a value >= mid_value.
lo = lo + 1
Do While list(lo) < mid_value
lo = lo + 1
If lo >= hi Then Exit Do
Loop
If lo >= hi Then
lo = hi
list(hi) = mid_value
Exit Do
End If
' Swap the lo and hi values.
list(hi) = list(lo)
Loop
' Sort the two sublists.
Quicksort list, min, lo - 1
Quicksort list, lo + 1, max
End Sub
|