1

This one sounds duplicate, but all the solutions given is not satisfying one of the requirement that is sorting by name. for instance

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 operating system sort them in a different way.

below solutions have tried already

var sorted = dirInfo.GetDirectories("*.*", SearchOption.TopDirectoryOnly).OrderBy(f => f.Name);

Array.Sort();
x2.
  • 9,554
  • 6
  • 41
  • 62
Jay
  • 1,869
  • 3
  • 25
  • 44
  • 1
    15 second search: possible duplicate of [C# How do I use Directory.GetFiles() to get files that have the same order as in Windows explorer?](http://stackoverflow.com/questions/11788981/c-sharp-how-do-i-use-directory-getfiles-to-get-files-that-have-the-same-order) , http://stackoverflow.com/questions/1012985/how-would-i-sort-a-list-of-files-by-name-to-match-how-windows-explorer-displays – Mitch Wheat Jan 13 '14 at 04:52
  • 3
    Have you tried the implementation linked to here: http://stackoverflow.com/a/1013055/2609288 ? – Baldrick Jan 13 '14 at 05:01
  • @Baldrick no, Let me give a shot and update – Jay Jan 13 '14 at 05:03
  • @Baldrick That solved the sort issue ( NumericComparer nc = new NumericComparer(); Array.Sort(strDir, nc);) however applying this to Directoryinfo[] is bit complex. – Jay Jan 13 '14 at 05:32
  • From what you've said, I'm not quite clear about what is solved and what isn't working. Can you update your question explaining the problem you're still having with the implementation you have now, with your current code? Without this, it's quite hard to help you further. – Baldrick Jan 13 '14 at 06:10
  • 1
    @Baldrick posted answer myself.but credit goes to you. – Jay Jan 13 '14 at 06:11
  • Glad you got it working. Never mind the credit, just happy to help! :) – Baldrick Jan 13 '14 at 06:33

1 Answers1

0

Thanks Baldrick for the valuable comment. using this ultimately solved the issue. there may be other ways but this is 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 similar to StrCmpLogicalW logical sorting in windows explorer API.

NumericComparer
StringLogicalComparer
Community
  • 1
  • 1
Jay
  • 1,869
  • 3
  • 25
  • 44