Solved

How to sort a webcontrols Listbox?

Posted on 2002-07-08
9
959 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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now