Skip to content

Instantly share code, notes, and snippets.

@catdad
Created December 10, 2014 14:17
Show Gist options
  • Save catdad/28bbc270fa6a2c9c86ce to your computer and use it in GitHub Desktop.
Save catdad/28bbc270fa6a2c9c86ce to your computer and use it in GitHub Desktop.
var sortDOM = (function(){
var sort = [].sort;
return function(elems, comparator) {
// Sort the elements.
// Make sure to get the pure elements array out of the jQuery wrapper.
var sortCollection = sort.call($(elems).get(), comparator);
// Check to make sure we have items in the collection
if (sortCollection.length === 0) {
return;
}
// Save the first element, and insert it as the first
var prev = sortCollection.shift();
$(prev).insertBefore(prev.parentNode.firstChild);
// Insert the rest of the elements in order
$(sortCollection).each(function(i, el) {
//$(el).insertAfter(prev);
el.parentNode.insertBefore(el, prev.nextSibling);
prev = el;
});
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment