I found and I knew how to sort an array based on another array of IDs but with the same range like this topic here.
I have an array like this:
matches = [
"league": {
"id":6,
"score": "score 1",
},
"league": {
"id": 9,
"score": "score 2",
},
"league": {
"id": 12,
"score": "score 3",
},
"league": {
"id": 6,
"score": "score 4",
},
"league": {
"id": 12,
"score": "score 5",
},
"league": {
"id": 3,
"score": "score 6",
},
"league": {
"id": 83,
"score": "score 7",
},
"league": {
"id": 76,
"score": "score 8",
},
]
The issue that I have a a very big complex data (above an example of data), I tried to sort this example following a list of IDs like that var ids =[6,12,3]
I did something like this:
result = [];
matches.forEach(function (a) {
result[ids.indexOf(a.league.id)] = a;
});
console.log(result);
But that give only the sort of three first IDs [6,12,3] and ignore the other IDs.
it's possible to sort the array like this:
matches = [
"league": {
"id":6,
"score": "score 1",
},
"league": {
"id": 6,
"score": "score 4",
},
"league": {
"id": 12,
"score": "score 3",
},
"league": {
"id": 12,
"score": "score 5",
},
"league": {
"id": 3,
"score": "score 6",
},
"league": {
"id": 9,
"score": "score 2",
},
"league": {
"id": 83,
"score": "score 7",
},
"league": {
"id": 76,
"score": "score 8",
},
]
The logic of sorting is to have ids cited in array of Ids first and then the rest of list of data with no changes?
matchesis an array, somatches(function (a) {...});does not even make sense to begin with.