title: How to sort depended objects by dependency
body: <p>I have a collection:</p>
  
  <pre><code>List&lt;VPair&lt;Item, List&lt;Item&gt;&gt; dependencyHierarchy;
  </code></pre>
  
  <p>The first item in pair is some object (item) and the second one is a collection of the same type objects that the first one depends on. I want to get a <code>List&lt;Item&gt;</code> in order of dependency, so there are not items that depend on the first element and so on (no cycled dependency!).</p>
  
  <p>Input: </p>
  
  <pre>
  Item4 depends on Item3 and Item5
  Item3 depends on Item1
  Item1 does not depend on any one
  Item2 depends on Item4 
  Item5 does not depend on any one 
  </pre>
  
  <p>Result:</p>
  
  <pre>
  Item1
  Item5
  Item3
  Item4
  Item2
  </pre>
  
  <p>Thank you.</p>
  
  <p><strong>SOLUTION:</strong> </p>
  
  <p><strong>Topological Sorting</strong> (thanks to <a href="https://stackoverflow.com/users/456106/loic-fevrier">Loïc Février</a> for idea) </p>
  
  <p>and</p>
  
  <p><a href="http://tawani.blogspot.com/2009/02/topological-sorting-and-cyclic.html" rel="noreferrer">example on C#</a>, <a href="http://www.java2s.com/Code/Java/Collections-Data-Structure/Topologicalsorting.htm" rel="noreferrer">example on Java</a>  (thanks to <a href="https://stackoverflow.com/users/61396/xcud">xcud</a> for great examples)</p>