JSFiddle - React, Tailwind, and code Playground

by jfriend00

HTML

<div id="main">
  <div id="dv_7">7</div>
  <div id="dv_1">1</div>
  <div id="dv_8">8</div>
  <div id="dv_4">4</div>
  <div id="dv_11">11</div>
  <div id="dv_2">2</div>
</div>

JavaScript

function sortChildrenDivsById(parentId) {
    var parent = document.getElementById(parentId);
    var children = parent.getElementsByTagName("div");
    var ids = [], i, len;
    for (i = 0, len = children.length; i < len; i++) {
        ids.push(parseInt(children[i].id.replace(/^.*_/g, ""), 10));
    }
    ids.sort(function(a, b) {return(a - b);});
     for (i = 0, len = ids.length; i < len; i++) {
         parent.appendChild(document.getElementById("dv_" + ids[i]));
     }
}

sortChildrenDivsById("main");