DotNetCurry Logo

Sort the Items of an ASP.NET DropDownList using jQuery

Posted by: Suprotim Agarwal , on 11/6/2009, in Category jQuery and ASP.NET
Views: 42387
Abstract: This short article demonstrates how to sort the items of an ASP.NET DropDownList using jQuery.
Sort the Items of an ASP.NET DropDownList using jQuery
This short article demonstrates how to sort the items of an ASP.NET DropDownList using jQuery. This article is a sample chapter from my EBook called 51 Tips, Tricks and Recipes with jQuery and ASP.NET Controls and has been modified to post it as an article.

Note that for demonstration purposes, I have included jQuery code in the same page. Ideally, these resources should be created in separate folders for maintainability.
Let us quickly jump to the solution and see how we can sort the items of an ASP.NET DropDownList using client-side code. This example uses the latest minified version of jQuery which is 1.3.2 that can be downloaded from here. This example assumes that you have a folder called "Scripts" with the jQuery file (jquery-1.3.2.min.js) in that folder.
<html xmlns="">
<head id="Head1" runat="server">
    <title>Sort Items of an ASP.NET DropDownList</title>
    <script src='../Scripts/jquery-1.3.2.min.js'
    <script type="text/javascript">
        $(function() {
            $('input[id$=btnSort]').click(function(e) {
                var sortedDdl =
                $.makeArray($('select[id$=DDL] option'))
                .sort(function(o, n) {
                    return $(o).text() < $(n).text() ? -1 : 1;
                $("#para").html("Items were Sorted!");
                $(this).attr("disabled", "disabled");
    <form id="form1" runat="server">
    <div class="smallDiv">
        <h2>Click on the Sort Button to Sort the DropDownList </h2>
        <asp:DropDownList ID="DDL" runat="server" >
            <asp:ListItem Text="Item3" Value="3"></asp:ListItem>
            <asp:ListItem Text="Item1" Value="1"></asp:ListItem>
            <asp:ListItem Text="Item4" Value="4"></asp:ListItem>
            <asp:ListItem Text="Item5" Value="5"></asp:ListItem>
            <asp:ListItem Text="Item2" Value="2"></asp:ListItem>
        <br /><br />
        <asp:Button ID="btnSort" runat="server" Text="Sort" />
        <p id="para"></p>
        <br /><br />
        Tip: Items are sorted in an Ascending order
    <br /><br /><br /><br /><br /><br />
    <div id="divDemo" class="w2">
    This demo is from my EBook
    <a href="">51 Tips, Tricks and Recipes using jQuery and ASP.NET Controls
    <br /><br />
    Visit <a href="">DotNetCurry</a> and
    <a href="">DevCurry</a> for more tips and tricks on jQuery
    and ASP.NET
In the code shown above, when the user clicks on the Sort button, the <option> elements are converted to an array using $.makeArray().
$.makeArray($('select[id$=DDL] option'))
The JavaScript built-in sort() function is used on this array, which does an in-place sort of the array.
$.makeArray($('select[id$=DDL] option'))
.sort(function(o, n) {
    return $(o).text() < $(n).text() ? -1 : 1;
The final step is to empty the contents of the DropDownList and then use the .html() to replace the existing <options> with the sorted <options>.
The val("1") sets the first option as selected, after the sorting has been done.
When the page loads, the output looks similar to the following screenshot:
Before clicking on the Sort button, the output is as shown below:
After clicking on the Sort Button, the output is as shown below:
Tip: To sort in a descending order, replace this line
$(a).text() < $(b).text()
with this:
$(a).text() > $(b).text()
You can see a Live Demo over here.
I hope you found this article useful and I thank you for viewing it. This article was taken from my EBook called 51 Tips, Tricks and Recipes with jQuery and ASP.NET Controls which contains similar recipes that you can use in your applications.
If you liked the article,  Subscribe to the RSS Feed or Subscribe Via Email  
Please share this article if you think it was worth reading. Thanks!
Share on Google+
Further Reading - Articles You May Like!
Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of DotNetCurry, DNC Magazine for Developers, SQLServerCurry and DevCurry. He has also authored a couple of books 51 Recipes using jQuery with ASP.NET Controls and a new one recently at The Absolutely Awesome jQuery CookBook.

Suprotim has received the prestigous Microsoft MVP award for nine times in a row now. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that represents premium web sites and digital publications comprising of Professional web, windows, mobile and cloud developers, technical managers, and architects.

Get in touch with him on Twitter @suprotimagarwal, LinkedIn or befriend him on Facebook

Page copy protected against web site content infringement 	by Copyscape

Feedback - Leave us some adulation, criticism and everything in between!
comments powered by Disqus
Comment posted by harishlavangade on Thursday, November 26, 2009 8:27 AM
It's very help for beginners.
Comment posted by apekshit on Sunday, December 27, 2009 11:30 PM
I want to use multiple select drop down in my site <a href=""> </a>