Organize file paths in C #

Is there a way to organize paths, depending on which one is more related to the root. For example, if I have ways:

"C:\someFolder\program files\b"
"C:\someFolder\X"
"C:\Z"
"C:\someFolder\program files\a"

then I would like to sort them as:

"C:\Z"
"C:\someFolder\X"
"C:\someFolder\program files\a"
"C:\someFolder\program files\b"

I'm actually trying to create a tree view and therefore want to sort them like this.

+3
source share
2 answers

What about:

files.OrderBy(x => x.Split('\\').Length).ThenBy(x => x)
+5
source

You can do

pathName.Split ('/'). Length

on the way to get the number of levels in depth, then sort by this number.

+1
source

All Articles