¡Hello! I have this object, and i’m trying to sort it by the amount of money they produce in a year ( Gross domestic product ) [GDP] and at the same time updates the property called rank from highest to lowest GDP.
const countryData = [
{ rank: 5, name: "Norway", gpd: 77975.4 },
{ rank: 3, name: "Macao", gpd: 81151.9 },
{ rank: 4, name: "Ireland", gpd: 77771.2 },
{ rank: 2, name: "Luxembourg", gpd: 113196.5 },
{ rank: 6, name: "Qatar", gpd: 69687.7 },
{ rank: 1, name: "Switzerland", gpd: 83716.8 },
];
Output should look like this:
const countryData = [
{ rank: 1, name: "Luxembourg", gpd: 113196.5 },
{ rank: 2, name: "Switzerland", gpd: 83716.8 },
{ rank: 3, name: "Macao", gpd: 81151.9 },
{ rank: 4, name: "Norway", gpd: 77975.4 },
{ rank: 5, name: "Ireland", gpd: 77771.2 },
{ rank: 6, name: "Qatar", gpd: 69687.7 },
];
I been trying with sort() method, like this:
const orderer = countryData.sort((a,b) =>
a.gpd < b.gpd ? 1 : -1);
console.log(orderer);
And works! but i have no clue of how reset and update rank, i know should be with map() method or for loop.
Thanks in advance!