Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to sort a webcontrols Listbox?

Posted on 2002-07-08
9
Medium Priority
?
1,050 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
Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 

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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…

618 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