1

I have two arrays which are parallel together (I don't know if it's the right term in English!). I mean the index 0 or 1 or ... of the first array represents the same index in the second one and vise versa...

Now I know how to sort the portionDurations array (one the arrays we have) based on start value like this:

var portionArrayEn = ["olaf", "enjoying", "your", "new", "permafrost"];

var portionDurations= [
  { end: "9", start: "6" },
  { end: "4", start: "2" },
  { end: "8", start: "5.5"},
  { end: "5", start: "3" },
  { end: "6", start: "5" },
];

portionDurations.sort((a, b) => (a.start > b.start) ? 1 : -1);

console.log(portionDurations);

Apparently this sorting may change the order of the portionDurations array while no change is done to portionArrayEn array. But we need to keep portionArrayEn in parallel. How can I do this?

Sara Ree
  • 3,417
  • 12
  • 48
  • Doesn't sort need to return `1`, `-1`, _and_ `0`? – evolutionxbox Feb 08 '21 at 11:51
  • What do you mean? – Sara Ree Feb 08 '21 at 11:53
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort - this is not to answer your question, but hopefully it helps a bit. – evolutionxbox Feb 08 '21 at 11:53
  • Have you also considered something like doing: `portionDurations.forEach((duration, i) => { duration.name = portionArrayEn[i]; });`. Now, the durations will contain the names and you can stop worrying about sorting two arrays simultaneously. – Djave Feb 08 '21 at 11:58

0 Answers0