Solved

How to sort a webcontrols Listbox?

Posted on 2002-07-08
9
1,024 Views
Last Modified: 2008-02-26
Hello,

I would like to know the simplest way to alphabetically sort the Listitems in a Webcontrols listbox?  I am using C#.

Thanks for your help.
0
Comment
Question by:moloko
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 23

Expert Comment

by:naveenkohli
ID: 7139482
simplest solution will be to store the list item values in an array. Sort that array alphabatecally and then bind your list to that array.
0
 
LVL 23

Accepted Solution

by:
naveenkohli earned 100 total points
ID: 7139494
Here is a proto type code.. You should be able to extend it to your needs..

private void Page_Load(object sender, System.EventArgs e)
          {
               wndListBox.Attributes.Add("size", "2");
               if (!IsPostBack)
               {
                    wndListBox.DataSource = this.CreateTestStringArray();
                    wndListBox.DataBind();
               }
          }

          private ArrayList CreateTestStringArray()
          {
               ArrayList myValues = new ArrayList();

               myValues.Add("Naveen");
               myValues.Add("Foo");
               myValues.Add("Bar");
               myValues.Add("Quax");

               myValues.Sort();
               return myValues;
          }
0
 

Author Comment

by:moloko
ID: 7139534
thanks for your response.  I did that for my Dropdownlist but was looking for another solution if possible.
If my data type is a ListItem in the Array, can i use the sort() method?  It threw an exception for me.
It works fine if i store strings in the array and sort them.  The reason i need ListItem is i have a text and a value for each.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:moloko
ID: 7139541
thanks for your response.  I did that for my Dropdownlist but was looking for another solution if possible.
If my data type is a ListItem in the Array, can i use the sort() method?  It threw an exception for me.
It works fine if i store strings in the array and sort them.  The reason i need ListItem is i have a text and a value for each.
0
 
LVL 23

Expert Comment

by:naveenkohli
ID: 7140258
In that case, you should construct your dropdown list dynamically. Meaning that on the page, you only have <asp:Dropdownlist> control and on Page_Load construct every ListItem object (pre-sorted) and then add each ListItem to it.

Naveen
0
 
LVL 1

Expert Comment

by:xloop
ID: 7140702
If it's coming straight from the DB, you can just use ORDER BY clause.


or manually do it:

   Listbox.MoverBars = true
   Listbox.Sorted = true
0
 

Author Comment

by:moloko
ID: 7142287
what do you mean by Listbox.Sorted?
I am using a webform listbox, i did not see anything about sorting.
I can sort easily for my dropdownlist since i can use an ArrayList to do the work and import the data later.
In the case for my listbox, it is more tricky because i have both a text and a value for each ListItem in the listbox.
0
 
LVL 4

Expert Comment

by:DarthPedro
ID: 7143921
You can compare the entries in an array for the listbox by calling the sort method on the array with a custom comparer object, like this:
    myValues.Sort(new MyTextComparer())

If the class in your array is defined as:
class MyType
{
    public string Text;
    public string Value;
}

Then, the comparer class would look something like this:
class MyTextComparer: IComparer
{
    public int Compare (object x, object y)
    {
        string tempObj1 = ((MyType)x).Text;
        string tempObj2 = ((MyType)y).Text;

        return tempObj1.CompareTo (tempObj2);
    }
}

If you need more information on array sorting and this method in particular, check out http://samples.gotdotnet.com/QuickStart/howto/default.aspx?url=/quickstart/howto/doc/sort.aspx.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 9405283
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered by naveenkohli

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

TheLearnedOne
EE Cleanup Volunteer
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question