Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sorting filelistbox

Status
Not open for further replies.

Oostwijk

Technical User
Oct 19, 2003
82
NL
I want to be able to sort a filelistbox (named file1) ascending and descending at name and size.

How can that be achieved ?
 
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
 
ok, but how can I sort that list on filesize then ?
 
Oostwijk,

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

Vladk
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top