SoFunction
Updated on 2025-04-13

Sort your files by array

When you use the FILESYSTEMOBJECT object to obtain a list of files in a directory, have you found that you cannot control how they are sorted, such as sorting by name, sorting by extension, sorting by file size, etc. Let's try to sort them in an array.
If you want to sort by name, it will be very simple, but if you want to sort by file size or file creation time, it will be a bit troublesome. We will do this with a 2D array.
The following code demonstrates how to achieve our goal by selecting the sorting method. Click the sort, click twice and then arrange it in reverse.

<HTML>
<HEAD>
<TITLE>File sorting demonstration</TITLE>
</HEAD>

<BODY>

<%
' Set a demo directory, :)

CONST DIRECTORY = "/"

' Define the sorting method with constants
CONST FILE_NAME = 0 'Sorted by name...and so on
CONST FILE_EXT = 1
CONST FILE_TYPE = 2
CONST FILE_SIZE = 3
CONST FILE_CREATED = 4
CONST FILE_MODIFIED = 5
CONST FILE_ACCESSED = 6

'Get the sort command, default to sort by name

req = Request("sortBy")
If Len(req) < 1 Then sortBy = 0 Else sortBy = CInt(req)
req = Request("priorSort")
If Len(req) < 1 Then priorSort = -1 Else priorSort = CInt(req)

'Set the reverse order
If sortBy = priorSort Then
reverse = true
priorSort = -1
Else
reverse = false
priorSort = sortBy
End If

' Next we start our real code. . .

path = ( DIRECTORY )

Set fso = CreateObject("")
Set theCurrentFolder = ( path )
Set curFiles =

' Make a loop for these files

Dim theFiles( )
ReDim theFiles( 500 ) ' I just set a size
currentSlot = -1 ' start before first slot

'We put all the relevant information of the file into the array

For Each fileItem in curFiles
fname =
fext = InStrRev( fname, "." )
If fext < 1 Then fext = "" Else fext = Mid(fname,fext+1)
ftype =
fsize =
fcreate =
fmod =
faccess =
currentSlot = currentSlot + 1
If currentSlot > UBound( theFiles ) Then
ReDim Preserve theFiles( currentSlot + 99 )
End If
' Put it in the array
theFiles(currentSlot) = Array(fname,fext,ftype,fsize,fcreate,fmod,faccess)
Next

' Now it's all in the array, start the next step


fileCount = currentSlot ' Number of files
ReDim Preserve theFiles( currentSlot )

' Sort
' (8 means string)

If VarType( theFiles( 0 )( sortBy ) ) = 8 Then
If reverse Then kind = 1 Else kind = 2 ' Sort the characters
Else
If reverse Then kind = 3 Else kind = 4 'Number, time. . .
End If

For i = fileCount TO 0 Step -1
minmax = theFiles( 0 )( sortBy )
minmaxSlot = 0
For j = 1 To i
Select Case kind
Case 1
mark = (strComp( theFiles(j)(sortBy), minmax, vbTextCompare ) < 0)
Case 2
mark = (strComp( theFiles(j)(sortBy), minmax, vbTextCompare ) > 0)
Case 3
mark = (theFiles( j )( sortBy ) < minmax)
Case 4
mark = (theFiles( j )( sortBy ) > minmax)
End Select
If mark Then
minmax = theFiles( j )( sortBy )
minmaxSlot = j
End If
Next

If minmaxSlot <> i Then

temp = theFiles( minmaxSlot )
theFiles( minmaxSlot ) = theFiles( i )
theFiles( i ) = temp
End If
Next
' Finish

%>
<FORM Name="doSort" Method="Get">
<INPUT Type=Hidden Name=priorSort Value="<% = priorSort %>">
<INPUT Type=Hidden Name=sortBy Value="-1">
</FORM>

<SCRIPT Language="JavaScript">
function reSort( which )
{
= which;
( );
}
</SCRIPT>

<CENTER>
<FONT Size="+2">
Show <% = (fileCount+1) %> Files in this directory <% = path %>
</FONT>
<P>
Click Sort, click Reverse Sort
<P>
<TABLE Border=1 CellPadding=3>
<TR>
<TH><A HREF="javascript:reSort(0);">File name</A></TH>
<TH><A HREF="javascript:reSort(1);">Extension</A></TH>
<TH><A HREF="javascript:reSort(2);">Type</A></TH>
<TH><A HREF="javascript:reSort(3);">Size</A></TH>
<TH><A HREF="javascript:reSort(4);">Creation time</A></TH>
<TH><A HREF="javascript:reSort(5);">Last modified time</A></TH>
<TH><A HREF="javascript:reSort(6);">Last access time</A></TH>
</TR>
<%

For i = 0 To fileCount
"<TR>" & vbNewLine
For j = 0 To UBound( theFiles(i) )
" <TD>" & theFiles(i)(j) & "</TD>" & vbNewLine
Next
"</TR>" & vbNewLine
Next
%>
</TABLE>

</BODY>
</HTML>