This question already has an answer here:
Is it possible, rather easily, to sort an array of objects by an array of IDs? Here's an example:
[{
id: "A",
name: "John"
}, {
id: "B",
name: "Bobby"
}, {
id: "C",
name: "Peter"
}]
Now I have an array of objects, each with an unique ID. I then have an array of IDs like so:
var ids = ["C", "A", "B"];
Would it be possible to sort the array of objects, so it ends up like this:
[{
id: "C",
name: "Peter"
}, {
id: "A",
name: "John"
}, {
id: "B",
name: "Bobby"
}]
You could order it with an object for the sort order.
If you have only the same amount of
id
in theids
array, then you could just rearrange the array with the assigned indices without sorting.