Sorting a Listbox

Neal

Forum Admin
Staff member
Joined
Jun 2, 2004
Messages
132
Location
VA
Programming Experience
10+
I have a listbox that reads a few files in a web site folder. Any suggestions on how to sort the listitems by creation time? I need to research sorting/string.compare routines I believe. If anyone has any code to share, that would save some time. Thanks!
 
Neal, One thing I have done in the past is to pre-sort the data in a dataview, prior to adding it to the listbox. Just make sure the listbox is not set to autosort and the items will remain in the order that you add them.

Darrell
 
Thanks, but I'm not using a DS/DV, just reading files with Directory.GetFiles() and loading them to the Listbox
 
Public Sub SortListbox()

Dim sKeys() As String

Dim sItems() As String

Dim iCount As Integer

sItems = System.IO.Directory.GetFiles("C:\")

ReDim sKeys(sItems.Length - 1)

For iCount = 0 To sItems.Length - 1

sKeys(iCount) = System.IO.File.GetCreationTime(sItems(iCount))

Next

sItems.Sort(sKeys, sItems)

lstFiles.Items.AddRange(sItems)

End Sub

 
Back
Top