You will probably have to turn off the parameters of the protocol string and the query, so +1 we recommend using the System.URI
class to handle this.
As for printing in the form of a tree, a direct approach is to use Dictionary<string, string>
to maintain the relationship of the child with the parent (value).
Another way is to use List<T>.Sort
, for example. eg:
public static void Print(List<string> list) { var path = new Stack<string>(); var count = new Stack<int>(); path.Push(""); count.Push(0); list.Sort(new Comparison<string>(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); } }
Unfortunately, p.campbell
right, it actually requires a normal comparison, which makes this implementation quite successful, but more cumbersome ( ?:-abuse
warning):
public static int UrlComparison(string x, string y) { if (x == null && y == null) return 0; if (x == null) return -1; if (y == null) return 1; for(int n = 0; n < 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 > cy) ? 1 : -1; } return (x.Length == y.Length) ? 0 : (x.Length > y.Length) ? 1 : -1; }
PS: Just to express a denial, I believe the Stacks logic is consize, but a little harder to understand. In a long-term project, I would stick to a parent dictionary.
source share