本文介绍了Directory.GetDirectories,按名称C#排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

这听起来很重复,但是给出的所有解决方案都不满足按名称排序的要求之一。例如

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

该方法返回J A1,J A10,J A11,J A2,J A3。但这是无法预期的,因为操作系统会以不同的方式对它们进行排序。

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();


推荐答案

感谢提出宝贵意见。使用此方法最终解决了问题。

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);

        }

从的实现类似于 StrCmpLogicalW 在Windows资源管理器API中进行逻辑排序。

Get the below class from codeproject implemented similar to StrCmpLogicalW logical sorting in windows explorer API.

NumericComparer
StringLogicalComparer

这篇关于Directory.GetDirectories,按名称C#排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 23:12