# 基于多个条件对多维数组进行排序

``````[
[1,3,5,7,9,22],
[1,200,300,400,500,600],
[1,2,3,4,5,6]
]
``````

``````[
[1,2,3,4,5,6],
[1,3,5,7,9,22],
[1,200,300,400,500,600]
]
``````

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);
``````

``````const data = [
[1,3,5,7,9,22],
[1,200,300,400,500,600],
[1,2,3,4,5,6]
]

let arr = [1,2,3,4,5,6,7,8,9,10];

function getCount(arr, set){
return arr.filter(x => set.has(x)).length
}
function sortOnCount(data, arr){
let set = new Set(arr);
return data.slice(0).sort((a, b) => getCount(b, set) - getCount(a, set))
}

console.log(sortOnCount(data, arr))``````

0 条评论