Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort an array of FileInfo[]

Tags:

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.

like image 402
JL. Avatar asked Jul 29 '09 09:07

JL.


3 Answers

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

like image 73
Jon Skeet Avatar answered Oct 17 '22 04:10

Jon Skeet


Array.Sort(taskFiles, delegate (FileInfo x, FileInfo y) {
    return string.Compare(x.Name,y.Name);
});
like image 32
Marc Gravell Avatar answered Oct 17 '22 05:10

Marc Gravell


    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);
            }
like image 23
RV. Avatar answered Oct 17 '22 04:10

RV.