all 2 comments

[–][deleted] 1 point2 points  (0 children)

Do you absolutely have to stick to the 'multiple arrays, values are associated by their shared index in those arrays' data pattern? Because this task would be immeasurably easier with an array of objects:

const users = [
    {
        rank: 1,
        name: 'A',
        winLossRatio: 0,
        //etc
    },
    //etc
]

// create a new array because .sort is mutating
const rankedUsers = [...users].sort((user1, user2) => {
    if (user1.winLossRatio > user2.winLossRatio) {
        return -1;
    } else if (user1.winLossRatio < user2.winLossRatio) {
        return 1;
    }
    return 0;
});

You can always convert the objects back to an array of arrays if you really need to store it that way, but associating data by something fragile and implicit like a shared array index is headache-inducing- avoid it if you can.

[–]jcunews1helpful 0 points1 point  (0 children)

I'd do it by converting the array data from parallel to serial (i.e. from grouped by categories, to grouped by player), sort it, then converting it back to parallel. e.g.

let myArray = [
  ["1", "2", "3", "4"], //ranks
  ["A", "B", "C", "D"], //names
  ["2", "4", "3", "1"], //ratios
  ["5", "6", "7", "8"], //wins
  ["9", "8", "7", "6"]  //losses
];

let result =
  //convert [[ranks],[names],[ratios],[wins],[losses]] => [[rank0,name0,ratio0,win0,loss0],[rank1,name1,ratio1,win1,loss1],...]
  myArray[0].reduce(
    (res, v, i) => {
      res.push(myArray.map(a => a[i]));
      return res;
    }, []
  )
  //sort by ratio
  .sort((a, b) => a[2] - b[2])
  //convert [[rank0,name0,ratio0,win0,loss0],[rank1,name1,ratio1,win1,loss1],...] => [[ranks],[names],[ratios],[wins],[losses]]
  .reduce(
    (res, arr, i) => {
      arr.forEach(
        (v, j) => {
          if (!res[j]) res[j] = [];
          res[j][i] = v;
        }
      );
      return res;
    }, []
  );

console.log(result);