Skip to content

Instantly share code, notes, and snippets.

@Swimburger
Created January 1, 2017 20:20
Show Gist options
  • Save Swimburger/7130929e813f53139a205fba938664b3 to your computer and use it in GitHub Desktop.
Save Swimburger/7130929e813f53139a205fba938664b3 to your computer and use it in GitHub Desktop.
jQuery sortChildren() extension
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<button>sort</button>
<ul>
<li>js</li>
<li>php</li>
<li>c#</li>
</ul>
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
<script>
(function(){
$.fn.sortChildren = function(sortCb){
this.children().sort(sortCb).appendTo(this);
return this;
};
$("button").click(function(){
$("ul").sortChildren(compareLanguages);
});
function compareLanguages(li1, li2){
return $(li1).text().localeCompare($(li2).text());
}
})();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment