seaweed
Jan 28th, 2000, 07:18 AM
Well, this is a lengthy answer, but basically what I would do is use a QuickSort routine to sort the array of file names after you obtain them.
Following is the code for the QuickSort (which you would put in a module), and a routine to get the file names (which I didn't test, so I can't guarantee it). I know the QuickSort works great, though, becuase I use it all the time.
By the way, I just copied and pasted this from my VB window, so the formatting may be a little off. Try cutting and pasting it into your VB window and see if it comes out alright. Ok, Here goes...
'***************************************************************************
'Put the following function into a module:
'***************************************************************************
Public Sub QuickSort(SortList As Variant, ByVal First As Integer, _
ByVal Last As Integer)
'
'Purpose: This function sorts a list from lowest to highest (0 - 9 for
' numeric arrays, A - z for string arrays). This is a recursive
' function, meaning that it calls itself in order to solve
' the problem.
'
'Arguments: SortList: An array of items to be sorted (Variant)
' First: The index of the first item in the array (Integer)
' Last: The index of the last item in the array (Integer)
'
'Note: There is no "return value". However, the argument list is
' returned with it's elements sorted. If you want to retain
' your original list, pass this function a copy of the list.
'
Dim Low As Integer, _
High As Integer, _
Temp As Variant, _
TestElement As Variant
Low = First
High = Last
TestElement = SortList((First + Last) / 2)
Do
Do While SortList(Low) < TestElement
Low = Low + 1
Loop
Do While SortList(High) > TestElement
High = High - 1
Loop
If (Low <= High) Then
Temp = SortList(Low)
SortList(Low) = SortList(High)
SortList(High) = Temp
Low = Low + 1
High = High - 1
End If
Loop While (Low <= High)
If (First < High) Then
Call QuickSort(SortList, First, High)
End If
If (Low < Last) Then
Call QuickSort(SortList, Low, Last)
End If
End Sub
'***************************************************************************
'Put the following code into the procedure that gets the file names:
'***************************************************************************
'Create a string array to hold file names
Dim FileName() As String, _
nCounter As Integer
nCounter = 1
'Enter first file name into array position 1
ReDim FileName(nCounter)
FileName(nCounter) = Dir("*.dat")
'If there was a file there, continue to enter the file
'names into the array (in any order)
Do While FileName(nCounter) <> ""
nCounter = nCounter + 1
ReDim Preserve FileName(nCounter)
FileName(nCounter) = Dir
Loop
'Remove the last item (empty string)
ReDim FileName(nCounter - 1)
'Use the QuickSort to sort the filenames alphabetically
Call QuickSort(FileName, 1, nCounter - 1)
'Now you have an array of file names sorted alphabetically!!
Following is the code for the QuickSort (which you would put in a module), and a routine to get the file names (which I didn't test, so I can't guarantee it). I know the QuickSort works great, though, becuase I use it all the time.
By the way, I just copied and pasted this from my VB window, so the formatting may be a little off. Try cutting and pasting it into your VB window and see if it comes out alright. Ok, Here goes...
'***************************************************************************
'Put the following function into a module:
'***************************************************************************
Public Sub QuickSort(SortList As Variant, ByVal First As Integer, _
ByVal Last As Integer)
'
'Purpose: This function sorts a list from lowest to highest (0 - 9 for
' numeric arrays, A - z for string arrays). This is a recursive
' function, meaning that it calls itself in order to solve
' the problem.
'
'Arguments: SortList: An array of items to be sorted (Variant)
' First: The index of the first item in the array (Integer)
' Last: The index of the last item in the array (Integer)
'
'Note: There is no "return value". However, the argument list is
' returned with it's elements sorted. If you want to retain
' your original list, pass this function a copy of the list.
'
Dim Low As Integer, _
High As Integer, _
Temp As Variant, _
TestElement As Variant
Low = First
High = Last
TestElement = SortList((First + Last) / 2)
Do
Do While SortList(Low) < TestElement
Low = Low + 1
Loop
Do While SortList(High) > TestElement
High = High - 1
Loop
If (Low <= High) Then
Temp = SortList(Low)
SortList(Low) = SortList(High)
SortList(High) = Temp
Low = Low + 1
High = High - 1
End If
Loop While (Low <= High)
If (First < High) Then
Call QuickSort(SortList, First, High)
End If
If (Low < Last) Then
Call QuickSort(SortList, Low, Last)
End If
End Sub
'***************************************************************************
'Put the following code into the procedure that gets the file names:
'***************************************************************************
'Create a string array to hold file names
Dim FileName() As String, _
nCounter As Integer
nCounter = 1
'Enter first file name into array position 1
ReDim FileName(nCounter)
FileName(nCounter) = Dir("*.dat")
'If there was a file there, continue to enter the file
'names into the array (in any order)
Do While FileName(nCounter) <> ""
nCounter = nCounter + 1
ReDim Preserve FileName(nCounter)
FileName(nCounter) = Dir
Loop
'Remove the last item (empty string)
ReDim FileName(nCounter - 1)
'Use the QuickSort to sort the filenames alphabetically
Call QuickSort(FileName, 1, nCounter - 1)
'Now you have an array of file names sorted alphabetically!!