Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You'll probably have to strip protocol and query string parameters, so +1 to the advise to use <code>System.URI</code> class to handle that.</p> <p>As for the printing it in tree-shape - a direct approach is to use a <code>Dictionary&lt;string, string&gt;</code> to keep association of child (key) to a parent (value).</p> <p>Another way is to take advantage of <code>List&lt;T&gt;.Sort</code>, e.g. like this:</p> <pre><code>public static void Print(List&lt;string&gt; list) { var path = new Stack&lt;string&gt;(); var count = new Stack&lt;int&gt;(); path.Push(""); count.Push(0); list.Sort(new Comparison&lt;string&gt;(UrlComparison)); foreach (var x in list) { while (!x.StartsWith(path.Peek())) { path.Pop(); count.Pop(); } count.Push(count.Pop() + 1); foreach(var n in count.Reverse()) Console.Write("{0}.", n); Console.WriteLine(" {0}", x); path.Push(x); count.Push(0); } } </code></pre> <p>Unfortunately, <code>p.campbell</code> is right, a custom comparison is actually required here, which makes this implementation still pretty performant, but more bulky (<code>?:-abuse</code> warning):</p> <pre><code>public static int UrlComparison(string x, string y) { if (x == null &amp;&amp; y == null) return 0; if (x == null) return -1; if (y == null) return 1; for(int n = 0; n &lt; Math.Min(x.Length, y.Length); n++) { char cx = x[n], cy = y[n]; if(cx == cy) continue; return (cx == '/' || cx == '.' || cx == '?') ? -1 : (cy == '/' || cy == '.' || cy == '?') ? 1 : (cx &gt; cy) ? 1 : -1; } return (x.Length == y.Length) ? 0 : (x.Length &gt; y.Length) ? 1 : -1; } </code></pre> <p>PS: Just to put a disclaimer, I feel that Stacks logic is consize, but a bit more complex to understand. In a long-term project, I'd stick with a child-parent dictionary.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload