Directory.GetDirectories, Sort by C # Name

It sounds duplicate, but all the solutions provided do not satisfy one of the requirements, which are sorted by name. eg

J A1 J A2 J A3 J A10 J A11 

The method returns J A1, J A10, J A11, J A2, J A3. But this is not expected, as the operating system sorts them differently.

We have already tried the solutions below

 var sorted = dirInfo.GetDirectories("*.*", SearchOption.TopDirectoryOnly).OrderBy(f => f.Name); Array.Sort(); 
0
source share
1 answer

Thanks to Baldrick for the valuable comment. using this eventually solved the problem. There may be other ways, but that's how I ended up.

  private void Walkdirectoryfulldepth(string dirPath, List<string> data) { DirectoryInfo dirInfo = new DirectoryInfo(dirPath); var sorted = dirInfo.GetDirectories("*.*", SearchOption.TopDirectoryOnly).ToList(); DirectoryInfo[] subDirs = dirInfo.GetDirectories("*.*", SearchOption.TopDirectoryOnly); string[] strDir=new string[subDirs.Count()]; int i =0; foreach (var item in subDirs) { strDir[i] = item.FullName; i++; } NumericComparer nc = new NumericComparer(); Array.Sort(strDir, nc); foreach (var item in strDir) { data.Add(Path.GetFileName(item)); Walkdirectoryfulldepth(item, data); } //foreach (var item in subDirs) // Walkdirectoryfulldepth(item.FullName, data); } 

Get the below class from codeproject , implemented similarly to the StrCmpLogicalW logical sorting in the Windows Explorer API.

 NumericComparer StringLogicalComparer 
0
source

All Articles