Sort ListBox

R

rn5a

Can the items in a ListBox be sorted by the name of the items?

The ListBox actually lists all directories & files existing in a
directory on the server. Note that all the directories should be listed
first followed by the files.
 
R

rn5a

David, this is how I am adding the directories & files in the ListBox:

Sub Page_Load(.....)
Dim fsi As FileSystemInfo
Dim dInfo As DirectoryInfo

dInfo = New DirectoryInfo(Server.MapPath("Folder1"))
For Each fsi In dInfo.GetFileSystemInfos
If ((fsi.Attributes And FileAttributes.Directory) = 16) Then
lstFD.Items.Add(New ListItem(fsi.Name & " <DIR>", fsi.Name
& "+D"))
Else
lstFD.Items.Add(New ListItem(fsi.Name & " <FILE>", fsi.Name
& "+F"))
End If
Next
SortListItems()
End Sub

Sub SortListItems()
Dim lItem As ListItem
Dim aList As ArrayList

aList = New ArrayList(lstFD.Items.Count)
For Each lItem In lstFD.Items
aList.Add(lItem)
Next

aList.Sort()
End Sub

Now how do I add the sorted ArrayList to the ListBox?

Note that the values of the items in the ListBox will not be exactly
the same as the text of the items. The text of each item will be the
directory name &/or file name but the value of each item will be the
directory name followed by the string '+D' &/or file name followed by
the string '+F' (both without quotes). In other words, if an item in
the ListBox is a directory, the value of that item will be the
directory name followed by '+D'. Similarly, if an item in the ListBox
is a file, its corresponding value will be the file name followed by
'+F'. I have done it this way so that I can easily differentiate which
item is a directory & which item is a file & then take action
accordingly when the Form submits.
 
R

rn5a

David, this is the revised code:

Sub Page_Load(.....)
Dim fsi As FileSystemInfo
Dim dInfo As DirectoryInfo

dInfo = New DirectoryInfo(Server.MapPath("Folder1"))
For Each fsi In dInfo.GetFileSystemInfos
If ((fsi.Attributes And FileAttributes.Directory) = 16) Then
'lstFD.Items.Add(New ListItem(fsi.Name & " <DIR>", fsi.Name
& "+D"))
Else
'lstFD.Items.Add(New ListItem(fsi.Name & " <FILE>",
fsi.Name & "+F"))
End If
aList.Add(fsi.Name)
Next

aList.Sort()

lstFD.DataSource = aList
lstFD.DataBind()
End Sub

The above code does sort the ListBox but as I had said, I would like to
first list all the directories & then list the files in the ListBox.

Moreover, using the above code, the value of each item remains exactly
the same as the text of the item in the ListBox but I want the values
of the items which are directories to be the directory names followed
by '+D'. Similarly, the values of the items which are files should be
the file names followed by '+F'.

What changes need I incorporate to implement these 2 features?
 
R

rn5a

David, I did it at last. This is the code:

Sub Page_Load(.....)
Dim i As Integer
Dim aList As ArrayList
Dim fsi As FileSystemInfo
Dim dInfo As DirectoryInfo
Dim strListItemValue As String

aList = New ArrayList
dInfo = New DirectoryInfo(Server.MapPath("Folder1"))
For Each fsi In dInfo.GetFileSystemInfos
If ((fsi.Attributes And FileAttributes.Directory) = 16) Then
'precede all directory names with 0
'so that they get listed before the files
aList.Add("0" & fsi.Name & "<DIR>")
Else
'precede all file names with 1 so that
'that they get listed after the directories
aList.Add("1" & fsi.Name & "<FILE>")
End If
Next

aList.Sort()

For i = 0 To aList.Count - 1
If (InStr(aList(i), "<DIR>") > 0) Then
'this item is a directory
strListItemValue = Left(aList(i), Len(aList(i)) - 5)
strListItemValue = Mid(strListItemValue, 2,
Len(strListItemValue)) & "+D"
Else
'this item is a file
strListItemValue = Left(aList(i), Len(aList(i)) - 6)
strListItemValue = Mid(strListItemValue, 2,
Len(strListItemValue)) & "+F"
End If

'get rid of the 0s & 1s preceding all the
'ListItems to display the file/directory name
lstFD.Items.Add(New ListItem(Mid(aList(i), 2, Len(aList(i))),
strListItemValue))
Next
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top