Sorting the filenames read from disk

Greetings.

I have been sucessfully using:

DirectoryInfo myDirectory = new DirectoryInfo(@dir);
FileInfo[] fileList = myDirectory.GetFiles( fileType );

to access files matching a given filetype.  But now I'd like to organize them in the list according to creation date and time.

What's the easiest way to do this?

Thanks.
ba272Asked:
Who is Participating?
 
b1xml2Connect With a Mentor Commented:
public class FilesComparer : IComparer
      {
            #region IComparer Members

            public int Compare(object x, object y)
            {
                  return DateTime.Compare(((FileInfo)x).CreationTime,((FileInfo)y).CreationTime);
            }

            #endregion
      }

Array.Sort(fileList,new FilesComparer());
0
 
b1xml2Commented:
using it this way, the files will be sorted according to ascending order.

if you want to sort according to descending order:


      
Comment from b1xml2
Date: 04/16/2005 04:10AM WST
      Your Comment       

public class FilesDescendingComparer : IComparer
     {
          #region IComparer Members

          public int Compare(object x, object y)
          {
               return DateTime.Compare(((FileInfo)y).CreationTime,((FileInfo)x).CreationTime);
          }

          #endregion
     }
Array.Sort(fileList,new FilesDescendingComparer());
0
 
ba272Author Commented:
Thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.