如何对文件信息数组进行排序[]

时间:2022-12-03 15:57:32

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.

这是如何做到的呢?使用。net v2可以快速且容易地完成。

4 个解决方案

#1


38  

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:

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

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:

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

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

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

#2


9  

Array.Sort(taskFiles, delegate (FileInfo x, FileInfo y) {
    return string.Compare(x.Name,y.Name);
});

#3


0  

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...

但是不管怎样,我认为GetFiles的结果已经按名称排序了……

#4


0  

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

#1


38  

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:

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

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:

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

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

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

#2


9  

Array.Sort(taskFiles, delegate (FileInfo x, FileInfo y) {
    return string.Compare(x.Name,y.Name);
});

#3


0  

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...

但是不管怎样,我认为GetFiles的结果已经按名称排序了……

#4


0  

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