Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This wasn't as trivial as I thought. <s>Probably the most sane solution (aside from building the list recursively) is to implement a comparer for this to do the sorting.</s></p> <p><s><pre> class DirectorySorter : IComparer&lt;string&gt; { public int Compare(string x, string y) { return StringComparer.Ordinal.Compare(x.Replace(Path.DirectorySeparatorChar, '\0'), y.Replace(Path.DirectorySeparatorChar, '\0')); var xPaths = x.Split(Path.DirectorySeparatorChar); var yPaths = y.Split(Path.DirectorySeparatorChar); var minLength = Math.Min(xPaths.Length, yPaths.Length); for (int i = 0; i &lt; minLength; i++) { var ires = xPaths[i].CompareTo(yPaths[i]); if (ires != 0) return ires; } var lres = xPaths.Length.CompareTo(yPaths.Length); if (lres == 0) { return lres; } else if (lres &lt; 0) { var i = y.LastIndexOf(Path.DirectorySeparatorChar); return x.Length == i ? lres : -lres; } else //if (lres > 0) { var i = x.LastIndexOf(Path.DirectorySeparatorChar); return y.Length == i ? lres : -lres; } } } </pre> </s></p> <p>(Seeing Steck's answer shows that I was nearly there with what I originally had. Just that I needed to use the Ordinal string comparer. So it turns out it works using that change.)</p> <p>On the other hand, we could use some properties of the directory structure to simplify this task and not implement a comparer.</p> <pre><code>var query = Directory .EnumerateDirectories(@"D:\Project\uploads", "*", SearchOption.AllDirectories) .OrderBy(name =&gt; name.Replace(Path.DirectorySeparatorChar, '\0'), StringComparer.Ordinal); </code></pre>
 

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