Solved

How to sort a webcontrols Listbox?

Posted on 2002-07-08
9
990 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
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

DevOps Toolchain Recommendations

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

Question has a verified solution.

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

Suggested Solutions

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

803 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