By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,909 Members | 2,016 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,909 IT Pros & Developers. It's quick & easy.

Sort ListBox

P: n/a
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.

Jan 10 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Not directly, though, if you add the items to an ArrayList first, and then
bind it to the Listbox, the arraylist is sort-able
Here's a code sample from ASPNet101.com:
http://aspnet101.com/aspnet101/aspne...code=arraylist

--
David Wier
MVP/ASPInsider
http://aspnet101.com
http://aspexpress.com

<rn**@rediffmail.comwrote in message
news:11**********************@i39g2000hsf.googlegr oups.com...
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.

Jan 10 '07 #2

P: n/a
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.
David Wier wrote:
Not directly, though, if you add the items to an ArrayList first, and then
bind it to the Listbox, the arraylist is sort-able
Here's a code sample from ASPNet101.com:
http://aspnet101.com/aspnet101/aspne...code=arraylist

--
David Wier
MVP/ASPInsider
http://aspnet101.com
http://aspexpress.com

<rn**@rediffmail.comwrote in message
news:11**********************@i39g2000hsf.googlegr oups.com...
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.
Jan 10 '07 #3

P: n/a
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...@rediffmail.com wrote:
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.
David Wier wrote:
Not directly, though, if you add the items to an ArrayList first, and then
bind it to the Listbox, the arraylist is sort-able
Here's a code sample from ASPNet101.com:
http://aspnet101.com/aspnet101/aspne...code=arraylist

--
David Wier
MVP/ASPInsider
http://aspnet101.com
http://aspexpress.com

<rn**@rediffmail.comwrote in message
news:11**********************@i39g2000hsf.googlegr oups.com...
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.
>
Jan 10 '07 #4

P: n/a
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
r...@rediffmail.com wrote:
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...@rediffmail.com wrote:
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.
David Wier wrote:
Not directly, though, if you add the items to an ArrayList first, and then
bind it to the Listbox, the arraylist is sort-able
Here's a code sample from ASPNet101.com:
http://aspnet101.com/aspnet101/aspne...code=arraylist
>
--
David Wier
MVP/ASPInsider
http://aspnet101.com
http://aspexpress.com
>
>
>
<rn**@rediffmail.comwrote in message
news:11**********************@i39g2000hsf.googlegr oups.com...
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.
Jan 11 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.