I need to sort a list that contains paths (relative or absolute) so that the deepest path appears first, for example:
\New Folder\Item1\tools\1
\New Folder\Item1\tools
\New Folder\Item1
\New Folder
etc...
Is there an API in Path class I can use to do it?
Thanks! J.
This is a bit out-of-the-box, but you could always do this:
var sortedList = list.OrderByDescending(
p => p.Count(c => c == Path.DirectorySeparatorChar
|| c == Path.AltDirectorySeparatorChar));
That is, simply order by how often the path separator character appears.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With