Home All Groups Group Topic Archive Search About

Display items from dropdown list in alphabetical order

Author
21 Jul 2005 6:16 AM
Siew Yee
Hi,

Is there any way where I can display items from dropdown list in
alphabetical order?
Thanks.

Author
21 Jul 2005 7:30 AM
Yahoo
ASP.NET framework does not have a sort method on the dropdown list as you
probably noticed. You have two options and they are about equally.

1) sort the list before you do your databind().

2) create a class that inherits from DropDownList and supply your own
method. You would probably want to do something like...

using System;
using System.Web;
using System.Web.UI.WebControls;
using System.Collections;

namespace JoesExample
{
public class MyDropDownList : System.Web.UI.WebControls.DropDownList
{
  public void Sort()
  {
   ListItemCollection lic = base.Items;
   ArrayList al = new ArrayList(lic);
   al.Sort(new MyComparer());

   lic.Clear();
   lic.AddRange( (ListItem[])al.ToArray(typeof(ListItem)) );
  }

  private class MyComparer : System.Collections.IComparer
  {
   public int Compare(object x, object y)
   {
    return new CaseInsensitiveComparer().Compare( ((ListItem)x).Text,
((ListItem)y).Text );
   }
  }
}
}

Joe
MCAD
SRE (Simple Rule Engine)
https://sourceforge.net/projects/sdsre/




Show quoteHide quote
"Siew Yee" <Siew***@discussions.microsoft.com> wrote in message
news:64C96226-0DE1-425C-B954-4D96BDC356A9@microsoft.com...
> Hi,
>
> Is there any way where I can display items from dropdown list in
> alphabetical order?
> Thanks.
>
>