JSFiddle - React, Tailwind, and code Playground

by mikecmpbll

HTML

<ul class="collection"></ul>

JavaScript

var indexes = [4,1,3,2,5,0],
    as_is = new Array();
for(i = 0; i<indexes.length; ++i) {
    var $collection = $(".collection"),
        $children = $collection.children(),
        index = indexes[i];
    if (as_is.length == 0) {
        as_is.push(index);
        console.log(as_is);
        $collection.append("<li>" + index + "</li>");
    } else {
        as_is.push(index);
        as_is.sort(function(a,b){return (a-b)});
        console.log(as_is);
        var ins = as_is.indexOf(index);
        var pos = $children.eq(ins);
        if (pos.length > 0) {
            $children.eq(ins).before("<li>" + index + "</li>");
            console.log("before");
        } else {
            $collection.append("<li>" + index + "</li>");
            console.log("after");
        }
    }
}