Skip to content

Instantly share code, notes, and snippets.

@mindplay-dk
Created October 4, 2013 12:52
Show Gist options
  • Save mindplay-dk/6825439 to your computer and use it in GitHub Desktop.
Save mindplay-dk/6825439 to your computer and use it in GitHub Desktop.
jQuery plugin to sort/order a list of HTML elements (including lists, tables, any element type or structure)
/**
* Sort a list of elements and apply the order to the DOM.
*/
jQuery.fn.order = function(asc, fn) {
fn = fn || function (el) {
return $(el).text().replace(/^\s+|\s+$/g, '');
};
var T = asc !== false ? 1 : -1,
F = asc !== false ? -1 : 1;
this.sort(function (a, b) {
a = fn(a), b = fn(b);
if (a == b) return 0;
return a < b ? F : T;
});
this.each(function (i) {
this.parentNode.appendChild(this);
});
};
@mindplay-dk
Copy link
Author

Working example here:

http://jsfiddle.net/mindplay/H2mrp/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment