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

时间:2009-07-29 09:24:09

标签: c# .net-2.0 .net

我有以下代码

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

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

如何使用.net v2尽可能快速轻松地完成此操作。

4 个答案:

答案 0 :(得分:39)

调用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)或任何其他比较字符串的方式:)

答案 1 :(得分:9)

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

答案 2 :(得分:0)

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

Array.Sort(taskFiles, comparison);

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

答案 3 :(得分: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);
            }