I need to arrange an array of objects consisting of a name and a list of dependencies (made from names).
An example of this array might be:
[
{ name: 'a', requires: ['b', 'c'] },
{ name: 'b', requires: ['c'] },
{ name: 'c', requires: [] },
]
I would like this array to be sorted so that elements requiring a specific set of dependencies are located after the necessary dependencies.
An array can contain more elements, I'm fine if the sort function throws an error in the case of circular dependencies.
Output Example:
[
{ name: 'c', requires: [] },
{ name: 'b', requires: ['c'] },
{ name: 'a', requires: ['b', 'c'] },
]
What is the most concise way to do this?
source
share