Search code examples
javascriptarraysobjectproperties

Replacing similar items of an array from the objects value


There's an object below named cricket mania consisting of net runs and points scored by different countries. I am trying to write a code in JS to sort the teams according to their Points first, and if they have the same points, use Net Run as the tiebreaker. In our case, its the Bangladesh and Nepal that has scored same points, so we will be considering the net runs of those two countries which are -1.176 of Bangladesh and -0.849 of Nepal. Since Nepal has got the highest net runs, we will arrange Nepal's net runs first. I have sorted the points but I am unable to figure out how do I replace the similar points by using net runs. I need the below array to be logged in to the console [ '3', '4', '5', '6', '-0.849', '-1.176' ].I would surely appreciate the time the people of this community would take to solve this little doubt that I am stucked in and would be more than happy to have someone to aid me out in this process of learning :)

This is the link to my codepen

``

    const cricketMania = {
    India: {
        netRuns: "1.319",
        points: "8"
    },
    Pakistan: {
        netRuns: "1.028",
        points: "6"
    },
    Saudi: {
        netRuns: "0.874",
        points: "5"
    },
    Nepal: {
        netRuns: "-0.849",
        points: "4"
    },
    Bangladesh: {
        netRuns: "-1.176",
        points: "4"
    },
    Zimbabwe: {
        netRuns: "-1.138",
        points: "3"
    }
};

var point = []; // [ '8', '6', '5', '8', '4', '3' ]
for (let m in cricketMania) {
    point.push(cricketMania[m]["points"]);
}

var sortedpoints = point.sort((a, b) => a - b);
document.write(sortedpoints); //[ '3', '4', '5', '6', '8', '8' ]

``


Solution

  • This is not the shortest possible solution, but it might give the point.

    1. We convert the objects to an array which contains name of the country and other values.
    2. Then we sort array by points and then by netRuns (if points are equal).

    The sorting also can be shortened to the construction like

    asArray.sort((a, b) => (a.points * 100 + a.netRuns) - (b.points * 100 + b.netRuns));
    

    where constant we use to multiply should be greater than module of the max b.netRuns.

    const cricketMania = {
        India: {
            netRuns: "1.319",
            points: "8"
        },
        Pakistan: {
            netRuns: "1.028",
            points: "6"
        },
        Saudi: {
            netRuns: "0.874",
            points: "5"
        },
        Nepal: {
            netRuns: "-0.849",
            points: "4"
        },
        Bangladesh: {
            netRuns: "-1.176",
            points: "4"
        },
        Zimbabwe: {
            netRuns: "-1.138",
            points: "3"
        }
    };
    
    const asArray = Object.entries(cricketMania).map(([key, val]) => ({
        name: key,
        netRuns: +val.netRuns,
        points: +val.points
    }));
    
    asArray.sort((a, b) => a.points > b.points ? 1 :
                    a.points < b.points ? -1 :
                    a.netRuns > b.netRuns ? 1 : 
                    a.netRuns < b.netRuns ? -1 : 0);
    console.log(asArray);