Created
October 4, 2013 12:52
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Working example here:
http://jsfiddle.net/mindplay/H2mrp/