Sorting a Listbox

Neal

Forum Admin
Staff member
Joined
Jun 2, 2004
Messages
123
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!
 

smithd3

Member
Joined
Jun 10, 2004
Messages
14
Programming Experience
10+
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
 

Neal

Forum Admin
Staff member
Joined
Jun 2, 2004
Messages
123
Location
VA
Programming Experience
10+
Thanks, but I'm not using a DS/DV, just reading files with Directory.GetFiles() and loading them to the Listbox
 

vbkev

Active member
Joined
Jun 4, 2004
Messages
32
Location
Newcastle, UK
Programming Experience
5-10
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

 
Top Bottom