Its already sorted by filename, if you wanted to sort by filesize you would have to use a second listbox after sorting the files to display the sorted list, you can hide the filelistbox if you dont want two lists
I don't think you could do it directly since List(ListIndex) property is read only. What you coud do is to sort files in two dim array and feed normal list box with the sorted first array columns. The first array column houses file names, the second- it's sizes. You would first need set reference to FSO (Microsoft Scripting Runtime library) for this trick (in Project/References).
Private Sub Command1_Click()
Dim arFiles() As String
Dim dblFileSize As Double
Dim strFileName As String
Dim fso As New FileSystemObject, fil As File
Dim i As Integer
Dim j As Integer
'File1 is your filelistbox
ReDim arFile(File1.ListCount, 1)
For i = 0 To File1.ListCount - 1
Set fil = fso.GetFile(File1.path & "\" & File1.List(i))
arFile(i, 0) = fil.Name
arFile(i, 1) = fil.Size
Next i
'Good old bubble sort
For i = 0 To File1.ListCount - 2
For j = 0 To File1.ListCount - 2 - i
If arFile(j + 1, 1) < arFile(j, 1) Then
strFileName = arFile(j, 0)
dblFileSize = arFile(j, 1)
arFile(j, 0) = arFile(j + 1, 0)
arFile(j, 1) = arFile(j + 1, 1)
arFile(j + 1, 0) = strFileName
arFile(j + 1, 1) = dblFileSize
End If
Next j
Next i
List1.Clear 'Normal list box
For j = 0 To File1.ListCount - 1
List1.AddItem arFile(j, 0)
Next j
End Sub
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.