1

¡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!

4
  • Deep clone, and sort the copy. Commented Oct 9, 2020 at 21:12
  • deep clone surely isn't necessary with this data. A shallow copy will do the job since there are no nested objects/arrays. Commented Oct 9, 2020 at 21:13
  • You have to deep clone since the user wants to alter the rank. Commented Oct 9, 2020 at 21:13
  • Actually there may be some confusion there. "but i have no clue of how reset and update rank" <= what exactly does that mean? I understand what update rank means. But what do you mean by "reset"? Commented Oct 9, 2020 at 21:17

3 Answers 3

2

After you sort the data, loop through the array and reassign the ranks.

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 },
];

const orderer = countryData.sort((a,b) => 
  a.gpd < b.gpd ? 1 : -1);

orderer.forEach((o, i) => o.rank = i+1);
console.log(orderer);

Sign up to request clarification or add additional context in comments.

Comments

0

Maybe like this?

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 }
]

// Name every function to make it testable
// (maybe later, but...):
const by = prop => (a, b) => a[prop] < b[prop] ? 1 : -1
const update = rank => (o, i) => Object.assign({}, o, o[rank] = ++i)

// Now you can do that task in universal,
// testable and readable way:
const ordered = countryData
  .sort(by('gpd'))
  .map(update('rank'))

console.log(ordered)

Comments

0

This was y solution. It looks cleaner:

let countryDataUpdate = countryData;
countryDataUpdate.sort((a,b) => a.gpd < b.gpd ? 1 : -1)
.map((value, index) => {
  value.rank = index + 1;
  return value;
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.