Created
December 10, 2014 14:17
-
-
Save catdad/28bbc270fa6a2c9c86ce to your computer and use it in GitHub Desktop.
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
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