ListItemCollection Sort Alphabetical

G

Guest

Hi,

I have this ListBox in which the Itemciollection can be manipulated by the
user (items can be added one way or another, and moved to other listboxes).
Is there any way that I can get this collection to be sorted alphabetically?
Thx
 
E

Emil Christopher Melar

benoit said:
Hi,

I have this ListBox in which the Itemciollection can be manipulated by the
user (items can be added one way or another, and moved to other listboxes).
Is there any way that I can get this collection to be sorted alphabetically?
Thx


ListItemCollection cannot be inherited from.

Therefore you have to write your own class implementing
System.Collections.IComparer.

Ideally you would make a ListItemComparer class implementing IComparer,
like this:

http://rafb.net/paste/results/BYaFKM60.html

Then you would copy all the ListItem references to an ArrayList, and
then use ArrayList.Sort with the given ListItemComparer.

When this is done, then you would clear the items in the ListBox and
re-add them from the ArrayList.
 
E

Emil Christopher Melar

Emil said:
ListItemCollection cannot be inherited from.

Therefore you have to write your own class implementing
System.Collections.IComparer.

Ideally you would make a ListItemComparer class implementing IComparer,
like this:

http://rafb.net/paste/results/BYaFKM60.html

Then you would copy all the ListItem references to an ArrayList, and
then use ArrayList.Sort with the given ListItemComparer.

When this is done, then you would clear the items in the ListBox and
re-add them from the ArrayList.

I was too quick.

Clearing the items will also make the references in the ArrayList
non-existant.

You have to _copy_ the ListItems in the Items property
(ListItemCollection) to the ArrayList:

ArrayList list = new ArrayList ( listBox.Items );
 
E

Emil Kvarnhammar

Performance hint:
You should try to apply the sorting to your underlying datastructure (the
list, dataset or whatever that you bound to the ListBox) and re-bind again,
rather than copying the ListBox items to an array (and then sort the array).

regards
Emil Kvarnhammar
http://www.ynax.com
 
G

Guest

Are you trying to see the list sorted while adding each new item or on click
of a button?

if you are trying to sort while adding items to the list, (i assume you are
using a code like ddList1.Items.Add(new ListItem("new item")) )

1) Init a counter

int ListItemCounter = 0;

2) loop through the list collection and incriment ListItemCounter

foreach (ListItem li in ddList1.Items)
{
ListItemCounter ++;
3) Check if Item.Text > "new item" , if so, insert new item at
ListItemCounter - 1

ddList1.Items.Insert(ListItemCounter-1,new ListItem("new item")) )

this logic will check each item currently in the list and will insert the
new item when it find a larger value..

The initial list should be sorted alphabetically for this to work..
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top