Click here to Skip to main content
13,300,002 members (70,134 online)
Click here to Skip to main content
Add your own
alternative version


19 bookmarked
Posted 19 Sep 2009

Custom Sorting with Jquery UI

, 19 Sep 2009
Rate this:
Please Sign up or sign in to vote.
In this article, I will examine how to build custom sorting Using Jquery UI. The jQuery UI Sortable plugin makes selected elements sortable by dragging with the mouse.


In this article, I will examine how to build custom sorting using Jquery UI. The jQuery UI Sortable plugin makes selected elements sortable by dragging with the mouse. Please see here for more details. In my demo application, I will create an XML file to store the custom settings that will hold the sorting order.

Using the Code

Here is our XML File:

<?xml version="1.0" encoding="utf-8"?>
<address>One Microsoft Way</address>
<address>Mountain View, CA</address>
<address>Seattle, WA</address>
<address>Armonk, New York</address>
<address>Sunnyvale, California</address>

I will create a simple web form application to demonstrate custom sorting. If you want to use ASP.NET MVC, then please see JQuery AJAX with ASP.NET MVC.

To create a web application, please follow these steps:

  1. Launch Visual Studio 2005 or 2008.
  2. Click on File -> new -> project, then select ASP.NET web application.
  3. It will create a basic web application template for us.

Here is our default.aspx page:

<%@ Page Language="C#" AutoEventWireup="true" 

	CodeBehind="Default.aspx.cs" Inherits="CustomSort._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 

<html xmlns="" >
<head runat="server">
<title>Sortable Demo</title> 
<script type="text/javascript" 

<script language="javascript" type="text/javascript">
google.load("jquery", "1.3.2");
google.load("jqueryui", "1.7.2");
<script type="text/javascript" 

<SCRIPT type=text/javascript>
$(function() {
function update() {
var list = new Array();
$("ul").each(function(index, id) {
var result = $('#' +'toArray');
var j = 0;
for (var i = 0; i < result.length; i++) {
list[j++] = $('#' + result[i])[0].value;
var jsonText = JSON.stringify({ list: list });
type: "POST",
url: "Default.aspx/SaveData",
data: jsonText,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(res) {

<style type="text/css"> 
ul.horizontal_list li{
list-style: none;
padding: 3px 10px 3px 10px;
margin: 5px;
border: 1px solid #CCC;
width: 250px; 
cursor : move;
<div id="divResult" style="color:red;"></div>
<asp:Panel ID="spaceHolder" runat="server" ></asp:Panel> 
<input type="button" value="Update" onclick="javascript:update();" /> 

In the above code:

  1. Our page is loading JQuery and JQuery UI from Google hosted AJAX libraries API as it will improve the site performance. Please see here for more details.
  2. Our page is also loading utility class from that has a method JSON.stringify(value) that takes a JavaScript value and produces a JSON text.
  3. We are using Jquery sortable UI plugin to read user defined sorting order. Please see here for more details.
  4. Our page makes jquery AJAX call using an HTTP POST request.
  5. It passes user defined sortorder data in JSON format to [WebMethod] as shown below.

Here is our code behind Default.aspx.cs:

namespace CustomSort
public partial class _Default : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
XDocument users = XDocument.Load(Server.MapPath("~/App_Data/Data.xml"));
var query = from user in users.Elements("customers").Elements("customer")
orderby user.Element("sortorder").Value 
select user;
StringBuilder sb = new StringBuilder();
int liCounter = 0;
sb.Append("<ul id=\"sortable\" class=\"horizontal_list\" 
foreach (XElement usr in query)
sb.AppendFormat("<li id=\"itemID{0}\" value=\"{1}\">", 
	liCounter++, usr.Element("id").Value);
sb.AppendFormat("{0},{1}</li>", usr.Element("name").Value, usr.Element("address").Value);
spaceHolder.Controls.Add(new LiteralControl(sb.ToString())); 
public static string SaveData(List<string> list)
string retResult = string.Empty;
XDocument xmldoc = XDocument.Load
int sortOrder = 1;
list.ForEach(delegate(string c)
XElement xElement = xmldoc.XPathSelectElement
			("customers/customer[id = " + c + "]/sortorder");
retResult = "Data has been saved successfully";
catch (Exception ex)
retResult = ex.Message; 
return retResult; 

In the above code, our SaveData method receives Customer ID in a list as a user defined sort order. Then we use LINQ to XML to update user defined sort order in an XML file.

Here are a few screen shots for our final application:





In this article, we examined custom sorting using jQuery UI. As you can see, jQuery UI is a very powerful tool to build Web 2.0 enabled applications.


  • 19th September, 2009: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Farooq Kaiser
Software Developer (Senior)
Canada Canada
16+ years of complete software development life cycle experience for web based applications, SOA, IOS and distributed, primarily using ASP.NET, MVC, Entity Framework, WEBAPI 2, LINQ, WCF, C#, XML, XSLT, AJAX, Winforms, JavaScript, JQuery, Angular JS, Typescript, Google APIs, C/C++, VB.NET, JAVA, Open XML, SSIS,SSRS, Amazon Web Services, Microsoft Azure, Python, PHP, Open Source Frameworks and design patterns.

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171207.1 | Last Updated 19 Sep 2009
Article Copyright 2009 by Farooq Kaiser
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid