473,397 Members | 1,950 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

ListItemCollection Sort Alphabetical

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

Nov 19 '05 #1
4 13374
benoit wrote:
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.

--
Emil Christopher Melar
Nov 19 '05 #2
Emil Christopher Melar wrote:
benoit wrote:
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.


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 );

--
Emil Christopher Melar
Nov 19 '05 #3
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

"benoit" <be****@discussions.microsoft.com> wrote in message
news:BB**********************************@microsof t.com...
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

Nov 19 '05 #4
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..
"benoit" wrote:
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

Nov 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: googleboy | last post by:
I didn't think this would be as difficult as it now seems to me. I am reading in a csv file that documents a bunch of different info on about 200 books, such as title, author, publisher, isbn,...
1
by: casey | last post by:
I am trying to bind a list collection with values to a radiobuttonlist, but when i try to access the selecteditem.value i always get the selecteditem.value private void Page_Load(object...
6
by: Kurt Mang | last post by:
Hi all -- I hate to yell "BUG", but can anyone explain why I cannot get a dropdown list / checkboxlist to recognise the Value property of a ListItemCollection (created at runtime) that I bind...
0
by: RAMADU | last post by:
Hi There is a ListItemCollection containing ListItems (of course), each of whose Value and Text properties have been set. The Value and Text's values are different i.e. ListItem.Text = "Name0"...
2
by: RAMADU | last post by:
Hi There is a ListItemCollection containing ListItems (of course), each of whose Value and Text properties have been set. The Value and Text's values are different i.e. ListItem.Text = "Name0"...
1
by: RichardH | last post by:
Hi, I have a ListItemCollection that I bind to DropDownList: ListItemCollection items = new ListItemCollection(); ListItem item; item = new ListItem("Option 1", "1"); items.Add(item); item =...
1
by: Randeh | last post by:
Using Visual Studio 2005, right now my only error (for now) is something with the function prototype that I can't figure out for the life of me. Every data type is unexpected for my function. I'm...
3
by: artev | last post by:
in the default sort which is the compare function used? alphabetical ordination is so:arry.sort(); but which is the default function used? is there a good link wich example for alphabetical...
3
by: eagerlearner | last post by:
I have the following code, which does not sort the list in alphabetical order, how can I sort everything in alphabetical order ? Before that, I want to ask, when an insertion occur, does it compare...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.