Back

Technologies:

javascriptjavascript
avatar
Tolerim
a month ago

How can I sort a two-dimensional array in JavaScript?

Given a 2D array, the goal is to sort it based on a particular column. The following logic currently sorts the "validationdate" column correctly:

var result = {
validationdate : ["2023-03","2022-11","2023-01","2023-04"],
program : ["a","a","b","a"],
}
result.validationdate.sort()
return result
However, the "program" column remains unsorted. To sort it similar to "validationdate," you can modify the code as follows:

var result = {
validationdate : ["2023-03","2022-11","2023-01","2023-04"],
program : ["a","a","b","a"],
}
var programIndex = result.program.map((value, index) => [value, index]);
programIndex.sort(([valueA, indexA], [valueB, indexB]) => {
  if (valueA !== valueB) {
    return valueA < valueB ? -1 : 1;
  }
  return indexA - indexB;
});
result.program = programIndex.map(([value, index]) => value);
return result;
The above modification will sort the "program" column similar to the "validationdate" column.

Note: The "validationdate" column is a string column, and both columns are contained in a JavaScript object.

Answers(2)
avatar
Vogi
a month ago
Verified Answer
Thank you for sharing the code snippet. The code you provided sorts the "validationdate" column correctly but not the "program" column. To sort the "program" column similar to the "validationdate" column, you can modify the code as follows:
var result = {
  validationdate : ["2023-03","2022-11","2023-01","2023-04"],
  program : ["a","a","b","a"],
}
var programIndex = result.program.map((value, index) => [value, index]);
programIndex.sort(([valueA, indexA], [valueB, indexB]) => {
  if (valueA !== valueB) {
    return valueA < valueB ? -1 : 1;
  }
  return indexA - indexB;
});
result.program = programIndex.map(([value, index]) => value);
return result;

The above modification will sort the "program" column similar to the "validationdate" column.

Note: The "validationdate" column is a string column, and both columns are contained in a JavaScript object.

avatar
Tolerim
a month ago
To sort the "program" column like the "validationdate" column, you can use the index of the sorted "validationdate" array to rearrange the "program" array. Here's an example code snippet:
var result = {
  validationdate: ["2023-03", "2022-11", "2023-01", "2023-04"],
  program: ["a", "a", "b", "a"],
};

var dateIndexes = result.validationdate.map((_, i) => i); // create an array of indexes
dateIndexes.sort((a, b) => result.validationdate[a].localeCompare(result.validationdate[b])); // sort the indexes based on the dates

result.validationdate.sort(); // sort the validationdate array

result.program = dateIndexes.map((index) => result.program[index]); // rearrange the program array based on the sorted indexes of validationdate

return result;
In this example code, we first create an array of indexes using the map() method. Then, we sort the indexes based on the sorted "validationdate" array using the sort() method with a custom compare function that uses localeCompare() to compare the dates. After that, we sort the "validationdate" array. Finally, we rearrange the "program" array based on the sorted indexes using the map() method.
;