0

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?

11
  • 1
    Don't use the JSON tag or the term for JavaScript literals. Your array isn't valid JSON syntax. Commented Jan 30, 2024 at 11:47
  • What's the sorting logic? Commented Jan 30, 2024 at 11:49
  • 1
    "I did something like this:" - matches is an array, so matches(function (a) {...}); does not even make sense to begin with. Commented Jan 30, 2024 at 11:54
  • 1
    @jabaa is sorting the Ids first but following the array of Ids and then keep the rest of data with no changes Commented Jan 30, 2024 at 12:01
  • 1
    The code defining matches above is invalid and throws a syntax error. Commented Jan 30, 2024 at 12:09

1 Answer 1

1

You could take an object for order by the id array.

For unknown id take a large value, like Infinity to keep this items at the end of the array.

const
    data = [{ 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" } }],
    ids = [6, 12, 3],
    order = Object.fromEntries(ids.map((id, i) => [id, i + 1]));

data.sort((a, b) =>
    (order[a.league.id] || Infinity) - (order[b.league.id] || Infinity)
);

console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.