What's the most efficient way to sort a linked list in Javascript/NodeJS? If I have an array of objects with an id and comesAfter which refers to the id of the previous item in the list, how can I put it in order?
ES6 features in the solution are fine to use. And it shouldn't modify the original array but return a new one. (But if it's not difficult to show how to do it in place that could help to see too.)
// before sorting
[
{ id: "three", comesAfter: "two" },
{ id: "one", comesAfter: null },
{ id: "four", comesAfter: "three" },
{ id: "two", comesAfter: "one" },
]
// after sorting
[
{ id: "one", comesAfter: null },
{ id: "two", comesAfter: "one" },
{ id: "three", comesAfter: "two" },
{ id: "four", comesAfter: "three" },
]