Created
January 1, 2017 20:20
-
-
Save Swimburger/7130929e813f53139a205fba938664b3 to your computer and use it in GitHub Desktop.
jQuery sortChildren() extension
This file contains 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
<!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