1

I have an array of players objects like this :

    var players = [{
        id: "thisIsID1",
        name: "William",
        otherProps
    },
    {
        id: "thisIsID2",
        name: "Shakespeare",
        otherProps
    },
    {
        id: "thisIsID3",
        name: "Lola",
        otherProps
    }]

And I have and array of their ID that has been shuffled, like so :

var shuffledIDs = ["thisIsID2", "thisIsID3", "thisIsID1"]

How can I sort the players var so the objects are in the same order as the corresponding IDs of shuffledIDs ?

Edit: different names just for the sake of making players different

4 Answers 4

6

If your data is short, then you can sort it with the following one-liner:

players = shuffledIDs.map(id => players.find(v => v.id == id))

Essentially, for every id in shuffledID, it finds the element in players with that id and puts it in the correct spot. However, this takes O(n^2) time so it might not scale well for larger data. If you want a faster method, you can maintain an object of IDs:

var ids = {};
players.forEach(v => ids[v.id] = v);
players = shuffledIDs.map(v => ids[v]);
Sign up to request clarification or add additional context in comments.

1 Comment

I think the latter is the most efficient method I've got ! Thanks a lot !
1

You can achieve it using array .find() method:

var players = [{
        id: "thisIsID1",
        name: "William"
    },
    {
        id: "thisIsID2",
        name: "Shakespeare"
    },
    {
        id: "thisIsID3",
        name: "Lola"
    }]

var shuffledIDs = ["thisIsID2", "thisIsID3", "thisIsID1"]
var result = shuffledIDs.map(x => players.find(p=>p.id === x))
console.log(result)

Comments

1

Create object with keys and values as index from shuffle array. Use sort method and prioritize bases above shuffled indexes. This way should even the case of duplicate data in players.

var players = [
  {
    id: "thisIsID1",
    name: "William"
  },
  {
    id: "thisIsID2",
    name: "Shakespeare"
  },
  {
    id: "thisIsID3",
    name: "Lola"
  }
];

const shuffleIds = ["thisIsID2", "thisIsID3", "thisIsID1"];

const shuf_idx = Object.fromEntries(shuffleIds.map((x, i) => [x, i]));

players.sort((a, b) => shuf_idx[a.id] - shuf_idx[b.id]);

console.log(players);

Comments

0

With .map() and .find() where using index of the element:

const shuffledIDs = ["thisIsID2", "thisIsID3", "thisIsID1"];
const players = [{ id: "thisIsID1", name: "William" }, { id: "thisIsID2",       name: "Shakespeare" }, { id: "thisIsID3", name: "Lola" }];

const result = players.map((e, i) => players.find(f => f.id === shuffledIDs[i]));

console.log(result);

1 Comment

If there is a value in shuffledIDs that doesn't match a value in players, this approach returns an undefined element in result array. Is there a way to prevent this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.