Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Simple ES6 utilities which come up on various projects
export function emptyNode(node) {
while (node.lastChild) {
export function sortChildren(container, sortKeyGenerator) {
Sort all child nodes in a given container using the provided
sortKeyExtractor function to obtain the sort key. The values should be
anything which will work with a simple < or > comparison; if you want a
reverse sort, multiply by -1 first.
// Since we will eventually be mutating the node list we will create an
// Array with the full results now:
let nodes = [...container.childNodes];
// Build an Array of [nodes array index, sort key] pairs so we can extract
// the values once and sort them in place:
let sortKeys = Array.from(, index) => [index, sortKeyGenerator(node)])
.sort(([, a], [, b]) => {
if (a < b) {
return -1;
} else if (a > b) {
return 1;
} else {
return 0;
.forEach(([index]) => {
let child = nodes[index];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.