473,788 Members | 2,816 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ListItemCollect ion 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 13440
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

ListItemCollect ion cannot be inherited from.

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

Ideally you would make a ListItemCompare r 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 ListItemCompare r.

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

ListItemCollect ion cannot be inherited from.

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

Ideally you would make a ListItemCompare r 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 ListItemCompare r.

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
(ListItemCollec tion) 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****@discuss ions.microsoft. com> wrote in message
news:BB******** *************** ***********@mic rosoft.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.A dd(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.I nsert(ListItemC ounter-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
6211
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, date and several other bits of info too. I can do a simple sort over the first field (title as it turns out), and that is fine as far as it gets:
1
3248
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 sender, System.EventArgs e) { // Put user code to initialize the page here if(!Page.IsPostBack)
6
3396
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 to it? This is a simplified version of what I have. I see no reason why this shouldn't work:
0
1290
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" ListItem.Value = "ID0" ListItem.Text = "Name1" ListItem.Value = "ID1"
2
12793
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" ListItem.Value = "ID0" ListItem.Text = "Name1" ListItem.Value = "ID1"
1
2955
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 = new ListItem("Option 2", "2"); item.Selected = true; items.Add(item);
1
5277
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 sure there's more errors but I'll figure those out if I can just get it to compile. #include <iostream> #include <cstring> using namespace std; const int MaxNames = 21; const int MaxChars = 16;
3
5410
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 order and also for multiarray order? (previous post)
3
3443
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 the string that I am inserting with every string inside the set list ? or just with string that is beside the location that is going to be inserted ? Thank you. #include <iostream> #include <hash_set> #include <string> using namespace std; ...
0
9656
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9498
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10110
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9967
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.