JSFiddle - React, Tailwind, and code Playground

by Glutamat

JavaScript

function sort (curr,prev) {
     var weight = {
        "[object Undefined]":6,         
        "[object Object]":5,
        "[object Null]":4,
        "[object String]":3,
        "[object Number]":2,
        "[object Boolean]":1
    }
    if (prev) { //mark the objects
        for (var i = prev.length,j,t;i>0;i--) {
            t = typeof (j = prev[i]);
            if (j != null && t === "object") {
                 j._pos = i;   
            } else if (t !== "object" && t != "undefined" ) break;
        }
    }

    curr.sort (sorter);
    
    if (prev && !sort.debug) {
        for (var k = prev.length,l,t;k>0;k--) {
            t = typeof (l = prev[k]);
            if (t === "object" && l != null) {
                delete l._pos;
            } else if (t !== "object" && t != "undefined" ) break;
        }
    }
    return curr;

    function sorter (a,b) {

         var tStr = Object.prototype.toString
         var types = [tStr.call(a),tStr.call(b)]
         var ret = [0,0];
         if (types[0] === types[1] && types[0] === "[object Object]") {
             if (prev) return a._pos - b._pos
             else {
                 return a === b ? 0 : 1;
             }
         } else if (types [0] !== types [1]){
                 return weight[types[0]] - weight[types[1]]
         }
         

        
        return a>b?1:a<b?-1:0;
    }

}
sort.debug = false;

var x = {
    a:"A Property",
    x:"24th Property",
    obj: {
        a: "A Property"
    },
    prim : 1,
}
x.obj.circ = x;

var y = {
    a:"A Property",
    x:"24th Property",
    obj: {
        a: "A Property"
    },
    prim : 1,
}
y.obj.circ = y;
var z = {};

var a = [1,"1",x,x,x,null,undefined,1,y,"2",x,z]
var b = ["1",1,z,x,x,y,undefined,1,null,"2",x,x]    
console.log (sort(a),sort(b,a))