[Solved] Sorting array of object based on range of Numbers of String [closed]


You can use first character to sort and consider if first character is number it is small,

let myObj = [{
    "60+": 0.1413972485314015,
    "18-29": 0.0832178903621611,
    "40-49": 0.1033361204013377,
    "30-39": 0.0835906328864075,
    "Under 18": 0.1326368677036551,
    "50-59": 0.1224973366151133
}];

const ordered = {};

Object.keys(myObj[0]).sort(
    function(a, b) {
        if (isNaN(Number(a.charAt(0))))
            return -1;
        if (isNaN(Number(b.charAt(0))))
            return 1;
        return a.charAt(0) - b.charAt(0);
    }

).forEach(function(key) {
    ordered[key] = myObj[0][key];
});
console.log(ordered);

2

solved Sorting array of object based on range of Numbers of String [closed]