title: JavaScript - Sort depending on dependency tree
body: <p>I must show a set of images that depend on each other. For example</p>
  <pre><code> Image A depends on no one
   Image B depends on A
   Image C depends on A and B
   Image D depends on F
   Image E depends on D and C
   Image F depends on no one
  </code></pre>
  <p>I have a javascript object like this:</p>
  <pre><code>const imageDependencies = {
      A: [],
      B: ['A'],
      C: ['A', 'B'],
      D: ['F'],
      E: ['D', 'C'],
      F: []
  }
  </code></pre>
  <p>I need to get all my image names ordered by their dependencies. The result of this example could be any of these:</p>
  <pre class="lang-js prettyprint-override"><code>//   so first yo get the value of A. Once you have it you can get the value of B. Once you have the value of A and B you can get C, and so on
  
  result_1 = [A, B, C, F, D, E] 
  
  // this could be another correct result
  result_2 = [A, F, D, B, C, E]
  </code></pre>
  <p>I've tried using the <code>Array.sort()</code> function like this:</p>
  <pre><code>let names = Object.keys(imageDependencies);
  names.sort((a,b) =&gt; {
      if(imageDependencies [a].includes(b)) return 1
      else return -1
  })
  </code></pre>
  <p>But is not working properly.</p>
  <p>How can this be done?</p>