Click here to Skip to main content
15,181,193 members
Articles / Web Development / ASP.NET
Posted 28 Sep 2003


28 bookmarked

Sort Method for an ASP.NET DropDownList

Rate me:
Please Sign up or sign in to vote.
3.50/5 (16 votes)
28 Sep 2003
This code will show you how to implement a sort feature on an ASP.NET DropDownList control.


I recently found a requirement in my ASP.NET application that needed to be able to sort a DropDownList control. Oddly enough I couldn't find a sort method within the framework, so I made my own. The code below shows what I did as a quick option, but you can of course use similar code to create a custom control that inherits from the DropDownList control.


Private Sub SortDropDown(ByVal dd As DropDownList)
    Dim ar As ListItem()
    Dim i As Long = 0
    For Each li As ListItem In dd.Items
        ReDim Preserve ar(i)
        ar(i) = li
        i += 1
    Dim ar1 As Array = ar

    ar1.Sort(ar1, New ListItemComparer)
End Sub
Private Class ListItemComparer _
    Implements IComparer

    Public Function Compare(ByVal x As Object, _
          ByVal y As Object) As Integer _
          Implements System.Collections.IComparer.Compare
        Dim a As ListItem = x
        Dim b As ListItem = y
        Dim c As New CaseInsensitiveComparer
        Return c.Compare(a.Text, b.Text)
    End Function
End Class


As I mentioned, this can easily be put into a custom control by simply creating a new class. Inherit from the DropDownList control and add the above code to the class. That's all. When you create your ASP.NET web form, you can drag and drop a normal DropDownList to your form, and edit the declaration in your code behind form, to use your custom class. Enjoy!


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
United States United States
I've been writing software applications since the early 90's from ASP and VB5 to C# and ASP.NET. I am currently working on law enforcement and criminal justice applications delivered over the web. I've always been astounded by the fact that the only 2 industries that refer to customers as "users" are narcotics and software development. Wink | ;-)

Comments and Discussions

GeneralMy vote of 4 Pin
RogerFraser17-Jul-13 21:38
MemberRogerFraser17-Jul-13 21:38 
GeneralDropDown List Selected Index Change Problem Pin
jahirhstu29-Aug-09 5:49
Memberjahirhstu29-Aug-09 5:49 
GeneralPreserve VALUE while sorting Pin
prouhiaw@guerrillamailblock.com30-Jul-09 1:18
Memberprouhiaw@guerrillamailblock.com30-Jul-09 1:18 
GeneralThanks! Pin
dsilk3-Jul-08 11:52
Memberdsilk3-Jul-08 11:52 
GeneralError Pin
halliej2x1-Aug-06 22:45
Memberhalliej2x1-Aug-06 22:45 
GeneralRe: Error Pin
Domenic2-Aug-06 3:27
MemberDomenic2-Aug-06 3:27 
GeneralRe: Error Pin
halliej2x2-Aug-06 20:16
Memberhalliej2x2-Aug-06 20:16 
GeneralRe: Error Pin
Domenic3-Aug-06 10:41
MemberDomenic3-Aug-06 10:41 
QuestionRe: Error Pin
Gregorio Cybill11-Aug-08 18:58
MemberGregorio Cybill11-Aug-08 18:58 
GeneralRe: Error Pin
Mohammed Aziz Elnahrawi5-Oct-06 21:31
MemberMohammed Aziz Elnahrawi5-Oct-06 21:31 
GeneralShortcut using CopyTo method on prealloced array Pin
hafthor27-Feb-06 6:22
Memberhafthor27-Feb-06 6:22 
GeneralRe: Shortcut using CopyTo method on prealloced array Pin
Louvaras4-Feb-09 4:43
MemberLouvaras4-Feb-09 4:43 
GeneralGood code, but let's make it even better Pin
HamidTheProgrammer9-Aug-05 11:34
MemberHamidTheProgrammer9-Aug-05 11:34 
GeneralRe: Good code, but let's make it even better Pin
Domenic10-Aug-05 3:08
MemberDomenic10-Aug-05 3:08 
GeneralWould just like to say... Pin
Anonymous4-Aug-05 1:51
MemberAnonymous4-Aug-05 1:51 
GeneralRe: Would just like to say... Pin
Domenic4-Aug-05 3:18
MemberDomenic4-Aug-05 3:18 
GeneralSyntax error Pin
chatelainj1-Sep-04 9:41
Memberchatelainj1-Sep-04 9:41 
GeneralRe: Syntax error Pin
Anonymous3-May-05 9:21
MemberAnonymous3-May-05 9:21 
Generalc# version wrapped in a SortableDropDownList Pin
Steinar Herland2-Apr-04 23:57
MemberSteinar Herland2-Apr-04 23:57 
GeneralRe: c# version wrapped in a SortableDropDownList Pin
Paul Rogan10-Jan-06 1:32
MemberPaul Rogan10-Jan-06 1:32 
GeneralRe: c# version wrapped in a SortableDropDownList Pin
banditking20-Apr-06 5:41
Memberbanditking20-Apr-06 5:41 
GeneralRe: c# version wrapped in a SortableDropDownList Pin
Ryan McFarren14-Jul-06 7:25
MemberRyan McFarren14-Jul-06 7:25 
GeneralRe: c# version wrapped in a SortableDropDownList Pin
ashushere8-Jun-07 12:56
Memberashushere8-Jun-07 12:56 
GeneralRe: c# version wrapped in a SortableDropDownList Pin
RicoRui18-Sep-07 22:15
MemberRicoRui18-Sep-07 22:15 
Questionhello!?? Pin
noahart24-Feb-04 23:05
Membernoahart24-Feb-04 23:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.