?
Solved

How to sort a webcontrols Listbox?

Posted on 2002-07-08
9
Medium Priority
?
1,033 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 400 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
Create CentOS 7 Newton Packstack Running Keystone

A bug was filed against RDO for the installation of Keystone v3. This guide is designed to walk you through the configuration for using Keystone v3 with Packstack. You will accomplish this using various repos and the Answers file.

 

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

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…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
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…
Suggested Courses

765 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