I'm trying to figure out an efficient method to sort a multidimensional array based on how many values of another array exist.
Given the following array:
[1,2,3,4,5,6,7,8,9,10]
I am trying to sort another array of arrays based on how many of those values are included.
[
[1,3,5,7,9,22],
[1,200,300,400,500,600],
[1,2,3,4,5,6]
]
So the code I'm trying to get to would return:
[
[1,2,3,4,5,6],
[1,3,5,7,9,22],
[1,200,300,400,500,600]
]
I think what I'm doing is very inefficient and could be written better or more succinctly with a method I'm not aware of?
https://jsfiddle.net/gb3fsLdv/
const compareNums = [1,2,3,4,5,6,7,8,9,10];
let ourData = [
[1,2,3,100,200,300],
[100,200,300,400,500,600],
[1,2,3,5,6,9]
];
function sortArr(compare, data){
let indexMatches = [];
data.map(arr => {
let count = 0;
compare.map(num => {
if(arr.includes(num)){
count++
}
})
indexMatches.push(count);
})
// So now I have indexMatches with a count of how many hits each array has in the correct index order as the original data
// And I can use data to sort them based on these values...
// Little stuck how to relate the two so the same sorting happens to both arrays
}
sortArr(compareNums, ourData);