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

What is the best way to sort a Web Forms Listbox as new items are added?

P: n/a
I have 2 listboxes on a Web Form.
As I move an item from 1 to the other it shows up at the end of the list.
How can I sort the list that just got the new item added to it so it is in
alphabetical order?
--
Joe Fallon

Nov 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
I came up with this generic routine that sorts a listbox Asc or Desc:

Private mList As New ArrayList

Private i As Integer

Private mItem As ListItem

================================================== =======================

Private Sub SortListBox(ByRef mylistbox As ListBox, ByVal Ascending As
Boolean)

mList.Clear()

For Each mItem In mylistbox.Items

mList.Add(mItem.Text)

Next

mList.Sort()

mylistbox.Items.Clear()

If Ascending = True Then

For i = 0 To mList.Count - 1

mylistbox.Items.Add(CStr(mList(i)))

Next

Else

For i = mList.Count - 1 To 0 Step -1

mylistbox.Items.Add(CStr(mList(i)))

Next

End If

End Sub
--
Joe Fallon

"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:eR**************@TK2MSFTNGP12.phx.gbl...
I have 2 listboxes on a Web Form.
As I move an item from 1 to the other it shows up at the end of the list.
How can I sort the list that just got the new item added to it so it is in
alphabetical order?
--
Joe Fallon


Nov 20 '05 #2

P: n/a
"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:eH****************@TK2MSFTNGP09.phx.gbl...
I came up with this generic routine that sorts a listbox Asc or Desc:


Hi Joe,

Your code worked of course but I noted a couple of things... couldn't
exactly figure out why mList and mItem should be private (they can be
declared in SortListBox) and such. Here is another way to approach the
solution however.

Note that I let the sort routine handle the sorting :-) You had it sorting
but then gathered up the items in reverse order to affect a descending sort.
By intercepting the sorting algorithm you can affect the sort in any manner.
Implementing case insensitive sorts of even sorting on some substring of the
text if desired.

I named mine "ListSort" but this is your SortListBox" changed a bit. It
takes a "Sorter" parameter instead of a boolean which represents the class
(and therefore the code) used to perform the sort. As a result the
reassignment to the listbox is identical regardless of which way the items
are sorted.

You call it this way:

ListSort(lst1, New AscendSorter)
or
ListSort(lst1, New DescendSorter)

and of course you can implement other sorter classes and simply pass them
along also.
Private Sub ListSort(ByRef lbox As ListBox, ByVal Sorter As IComparer)

Dim mList As New ArrayList

With mList

Dim mItem As ListItem

For Each mItem In lbox.Items
.Add(mItem.Text)
Next

.Sort(Sorter)

End With

With lbox

.Items.Clear()

Dim i As Integer
For i = 0 To (mList.Count - 1)
.Items.Add(CStr(mList(i)))
Next

End With

End Sub
Private Class AscendSorter
Implements IComparer

Public Function Compare(ByVal x As Object, ByVal y As Object) _
As Integer Implements System.Collections.IComparer.Compare

Dim xx As String = CType(x, String)
Dim yy As String = CType(y, String)

If xx > yy Then
Compare = 1
Else
If xx < yy Then
Compare = -1
Else
Compare = 0
End If
End If

End Function

End Class

Private Class DescendSorter
Implements IComparer

Public Function Compare(ByVal x As Object, ByVal y As Object) _
As Integer Implements System.Collections.IComparer.Compare

Dim xx As String = CType(x, String)
Dim yy As String = CType(y, String)

If yy > xx Then
Compare = 1
Else
If yy < xx Then
Compare = -1
Else
Compare = 0
End If
End If

End Function

End Class
Nov 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.