如何对 FileInfo[] 数组进行排序

发布于 2024-07-29 17:02:22 字数 243 浏览 6 评论 0原文

我有以下代码

DirectoryInfo taskDirectory = new DirectoryInfo(this.taskDirectoryPath);
FileInfo[] taskFiles = taskDirectory.GetFiles("*" + blah + "*.xml");

我想按文件名对列表进行排序。

这是如何使用 .net v2 尽可能快速、轻松地完成的。

I have the following code

DirectoryInfo taskDirectory = new DirectoryInfo(this.taskDirectoryPath);
FileInfo[] taskFiles = taskDirectory.GetFiles("*" + blah + "*.xml");

I would like to sort the list by filename.

How is this done, as quickly and easily as possible using .net v2.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

焚却相思 2024-08-05 17:02:22

调用 Array.Sort,传入比较委托:

Array.Sort(taskFiles, delegate(FileInfo f1, FileInfo f2) {
    return f1.Name.CompareTo(f2.Name);
});

在 C# 3 中,这变得稍微简单一些:

Array.Sort(taskFiles, (f1, f2) => f1.Name.CompareTo(f2.Name));

或者,如果您想使用不区分大小写的排序顺序,则可以使用 StringComparer:(

Array.Sort(taskFiles,
           (x, y) => StringComparer.OrdinalIgnoreCase.Compare(x.Name, y.Name));

或使用 string.Compare(x.Name, y.Name, true),或任何其他比较字符串的方法:)

Call Array.Sort, passing in a comparison delegate:

Array.Sort(taskFiles, delegate(FileInfo f1, FileInfo f2) {
    return f1.Name.CompareTo(f2.Name);
});

In C# 3 this becomes slightly simpler:

Array.Sort(taskFiles, (f1, f2) => f1.Name.CompareTo(f2.Name));

Or you can use a StringComparer if you want to use a case-insensitive sort order:

Array.Sort(taskFiles,
           (x, y) => StringComparer.OrdinalIgnoreCase.Compare(x.Name, y.Name));

(or use string.Compare(x.Name, y.Name, true), or any of the many other ways of comparing strings :)

但可醉心 2024-08-05 17:02:22
Array.Sort(taskFiles, delegate (FileInfo x, FileInfo y) {
    return string.Compare(x.Name,y.Name);
});
Array.Sort(taskFiles, delegate (FileInfo x, FileInfo y) {
    return string.Compare(x.Name,y.Name);
});
和影子一齐双人舞 2024-08-05 17:02:22
    public class FileComparer : IComparer
        {
            public enum CompareBy
            {
                Name /* a-z */,
                LastWriteTime /* oldest to newest */,
                CreationTime  /* oldest to newest */,
                LastAccessTime /* oldest to newest */,
                FileSize /* smallest first */,

            }
            // default comparison
            int _CompareBy = (int)CompareBy.Name;
            public FileComparer()
            {
            }

            public FileComparer(CompareBy compareBy)
            {
                _CompareBy = (int)compareBy;
            }
            int IComparer.Compare( object x, object y )
            {
                int output = 0;
                DirectoryInfo file1 = new DirectoryInfo(x.ToString());
                DirectoryInfo file2 = new DirectoryInfo(y.ToString());
                switch(_CompareBy)
                {
                    case (int)CompareBy.LastWriteTime:
                        output = DateTime.Compare(file1.LastWriteTime, file2.LastWriteTime);
                        break;
                    case (int)CompareBy.CreationTime:
                        output = DateTime.Compare(file1.CreationTime, file2.CreationTime);
                        break;
                    case (int)CompareBy.LastAccessTime:
                        output = DateTime.Compare(file1.LastAccessTime, file2.LastAccessTime);
                        break;
    //              case (int)CompareBy.FileSize:
    //                  output = Convert.ToInt32(file1.Length - file2.Length);
    //                  break;
                    case (int)CompareBy.Name:
                    default:
                        output = (new CaseInsensitiveComparer()).Compare( file1.Name, file2.Name );
                        break;
                }
                return output;
            }
        }

///////////////////////////////////
ArrayList list=new ArrayList();         
            string folder = @"D:\DVRData\ICICI\Transaction\21-Dec-08\08_51_23_2231";
            string[] files = Directory.GetFiles(folder);            
            IComparer comp = new FileComparer(FileComparer.CompareBy.CreationTime);
            Array.Sort(files, comp);
            foreach(string file in files)
            {
                list.Add(file);
            }
    public class FileComparer : IComparer
        {
            public enum CompareBy
            {
                Name /* a-z */,
                LastWriteTime /* oldest to newest */,
                CreationTime  /* oldest to newest */,
                LastAccessTime /* oldest to newest */,
                FileSize /* smallest first */,

            }
            // default comparison
            int _CompareBy = (int)CompareBy.Name;
            public FileComparer()
            {
            }

            public FileComparer(CompareBy compareBy)
            {
                _CompareBy = (int)compareBy;
            }
            int IComparer.Compare( object x, object y )
            {
                int output = 0;
                DirectoryInfo file1 = new DirectoryInfo(x.ToString());
                DirectoryInfo file2 = new DirectoryInfo(y.ToString());
                switch(_CompareBy)
                {
                    case (int)CompareBy.LastWriteTime:
                        output = DateTime.Compare(file1.LastWriteTime, file2.LastWriteTime);
                        break;
                    case (int)CompareBy.CreationTime:
                        output = DateTime.Compare(file1.CreationTime, file2.CreationTime);
                        break;
                    case (int)CompareBy.LastAccessTime:
                        output = DateTime.Compare(file1.LastAccessTime, file2.LastAccessTime);
                        break;
    //              case (int)CompareBy.FileSize:
    //                  output = Convert.ToInt32(file1.Length - file2.Length);
    //                  break;
                    case (int)CompareBy.Name:
                    default:
                        output = (new CaseInsensitiveComparer()).Compare( file1.Name, file2.Name );
                        break;
                }
                return output;
            }
        }

///////////////////////////////////
ArrayList list=new ArrayList();         
            string folder = @"D:\DVRData\ICICI\Transaction\21-Dec-08\08_51_23_2231";
            string[] files = Directory.GetFiles(folder);            
            IComparer comp = new FileComparer(FileComparer.CompareBy.CreationTime);
            Array.Sort(files, comp);
            foreach(string file in files)
            {
                list.Add(file);
            }
笑叹一世浮沉 2024-08-05 17:02:22
Comparison<FileInfo> comparison = new Comparison<FileInfo>(delegate(FileInfo a, FileInfo b)
{
    return String.Compare(a.Name, b.Name);
});

Array.Sort(taskFiles, comparison);

但无论如何我认为 GetFiles 的结果已经按名称排序了......

Comparison<FileInfo> comparison = new Comparison<FileInfo>(delegate(FileInfo a, FileInfo b)
{
    return String.Compare(a.Name, b.Name);
});

Array.Sort(taskFiles, comparison);

But anyway I think the result of GetFiles is already sorted by name...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文