Sort DOM elements
$ component install component/sort
Sort element ascending with the given callback function.
els
is optional and defaults to el.children
Alias of sort(el, els, fn)
Sort descending, inverting the fn()
return value.
<ul>
<li>Tobi</li>
<li>Jane</li>
<li>Abby</li>
<li>Loki</li>
<li>Simon</li>
<li>Manny</li>
<li>Luna</li>
</ul>
<button onclick='asc()'>Sort ascending</button>
<button onclick='desc()'>Sort descending</button>
<script src="build/build.js"></script>
<script>
var sort = require('sort');
var ul = document.querySelector('ul');
function alpha(a, b){
a = a.textContent;
b = b.textContent;
if (a < b) return -1;
if (a > b) return 1;
return 0;
}
function asc() {
sort(ul, alpha);
}
function desc() {
sort.desc(ul, alpha);
}
</script>
MIT