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

How do I sort the items in a list box?

P: n/a
How do I sort the items in a list box?

I am using a class derived from IComparer to sort items on columns in a
ListView, but the ListBox doesn't support this kind of facility. The
"items" in my list box aren't just strings, they are a class I have defined
for each list item. There is a comparitor function, but how can I write -
ListBox.Items.Sort() in this instance?

Do I need to do my own quicksort?
Nov 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Hi

I wrote a generic Comparer class for sorting collections of objects. Check
out the code:
http://weblogs.asp.net/jan/posts/6479.aspx
--
Greetz,
Jan
__________________________________
Read my weblog: http://weblogs.asp.net/jan

"Robin Tucker" <id*************************@reallyidont.com> schreef in
bericht news:bq*******************@news.demon.co.uk...
How do I sort the items in a list box?

I am using a class derived from IComparer to sort items on columns in a
ListView, but the ListBox doesn't support this kind of facility. The
"items" in my list box aren't just strings, they are a class I have defined for each list item. There is a comparitor function, but how can I write -
ListBox.Items.Sort() in this instance?

Do I need to do my own quicksort?

Nov 20 '05 #2

P: n/a
* "Robin Tucker" <id*************************@reallyidont.com> scripsit:
How do I sort the items in a list box?


Sort them before adding them to the control.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #3

P: n/a
Hi,

Thanks for this - although I have written a "generic" comparer (well it
compares 2 instances of the objects I want it to compare, on the keys I want
it to compare on). I'm using this in the ListView to sort, but the ListBox
is different. With the list view, all I need to do is set the comparer when
the user clicks on a column and it sorts automatically. Listbox's are more
primitive. I just have a ListBox.ObjectCollection, which doesn't support
sorting, so I have to do it myself somehow. Just thought there might be a
quick and easy way like with the ListView.

Cheers
"Jan Tielens" <ja*@no.spam.please.leadit.be> wrote in message
news:uq**************@TK2MSFTNGP12.phx.gbl...
Hi

I wrote a generic Comparer class for sorting collections of objects. Check
out the code:
http://weblogs.asp.net/jan/posts/6479.aspx
--
Greetz,
Jan
__________________________________
Read my weblog: http://weblogs.asp.net/jan

"Robin Tucker" <id*************************@reallyidont.com> schreef in
bericht news:bq*******************@news.demon.co.uk...
How do I sort the items in a list box?

I am using a class derived from IComparer to sort items on columns in a
ListView, but the ListBox doesn't support this kind of facility. The
"items" in my list box aren't just strings, they are a class I have

defined
for each list item. There is a comparitor function, but how can I write - ListBox.Items.Sort() in this instance?

Do I need to do my own quicksort?


Nov 20 '05 #4

P: n/a
Cor
Hi Robin,

With a little bit changed the code that EricJ did supply us today I got with

Dim li As clsListItem = New clsListItem("Robbedoes", "Marsupulanie")
ListBox1.Items.Add(li)
li = New clsListItem("Suske", "wiske")
ListBox1.Items.Add(li)
li = New clsListItem("Lucky Luke", "Joly Jumper")
ListBox1.Items.Add(li)
ListBox1.Sorted = True

On screen as:
Lucky Luke,
Robbedoes
Suske

I hope this is something for you,

Cor
Nov 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.