Sort Items of a BulletedList Control using jQuery

Posted by: Suprotim Agarwal , on 5/25/2010, in Category jQuery and ASP.NET
Views: 77661
Abstract: This article demonstrates how to sort the items of the BulletedList control and display the sorted list.
This article demonstrates how to sort the items of the BulletedList control and display the sorted list.
This article is a chapter from my EBook called 51 Recipes with jQuery and ASP.NET Controls. The chapter has been modified a little to publish it as an article.
Note that for demonstration purposes, I have included jQuery code and css in the same page. Ideally, these resources should be created in separate folders for maintainability. The code shown below has been tested on IE7, IE8, Firefox 3, Chrome 2 and Safari 4
Let us quickly jump to the solution and see how to sort the items of the BulletedList control.
<html xmlns="">
<head id="Head1" runat="server">
    <title>Sort Items of a Bulleted List Control</title>
    <style type="text/css">
            font-family:"Lucida Sans Unicode", "Lucida Grande", Verdana, Arial;
            border:solid 1px #c6cfe1;
   <script src=""
    <script type="text/javascript">
        $(function() {
            $("input[id$=btnSort]").click(function(e) {
                var sortedList = $.makeArray($('.bullet li'))
                    .sort(function(o, n) {
                        return ($(o).text() < $(n).text()) ? -1 : 1;
                    $(this).attr("disabled", "disabled");
    <form id="form1" runat="server">
    <div class="smallDiv">
        <h2>Click on the Button to Sort List Items</h2>
        <asp:BulletedList ID="list" runat="server" class="bullet">
            <asp:ListItem Value="dnc">
            <asp:ListItem Value="sql">
            <asp:ListItem Value="devc">
            <asp:ListItem Text="st">
        <br /><br />
        <asp:Button ID="btnSort" runat="server"
            Text="Sort Items"
            ToolTip="Sort the Bulleted List Items in Ascending order" />  
When the user clicks on the Sort button, the BulletedList items are converted to an array using $.makeArray(). The JavaScript built-in sort() function is used on this array, which does an in-place sort of the array.
var sortedList = $.makeArray($('.bullet li'))
 .sort(function(o, n) {
      return ($(o).text() < $(n).text()) ? -1 : 1;
The final step is to use .html() to replace the existing list items with the sorted list items.
The BulletedList, before the sorting is done, looks like this:
UnSorted Bulleted List
The BulletedList, after the sorting is done, looks like this:
Sorted BulletedList
Note:To sort in a descending order, replace this line
$(o).text() < $(n).text()
with this:
$(o).text() > $(n).text()
You can see a Live Demo
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. 

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.

We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).

Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.

Click here to Explore the Table of Contents or Download Sample Chapters!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

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 The Absolutely Awesome jQuery CookBook.

Suprotim has received the prestigious Microsoft MVP award for ten consecutive times. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that offers Digital Marketing and Branding services to businesses, both in a start-up and enterprise environment.

Get in touch with him on Twitter @suprotimagarwal or at LinkedIn

Page copy protected against web site content infringement 	by Copyscape

Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by Fox on Saturday, June 19, 2010 5:02 AM
Excuse me, the image of "after sorting" is wrong. Feel free to delete this comment (or not aprove it if are moderated) if you fix the error :)
Comment posted by Bhupendar Singh on Wednesday, January 11, 2012 6:01 AM
Very informative post. It’s really helpful for me and helped me lot to complete my task. Thanks for sharing with us. I had found another nice post over the internet which was also explained very well about Bulleted List Control in ASP.Net, for more details of this post check out this link…

Thanks everyone for your precious post!!
Comment posted by Archit on Thursday, December 6, 2012 4:27 AM
Sir, The images you illustrated appears same. So it is difficult to understand how sorting is done.