?
Solved

Sorting the filenames read from disk

Posted on 2005-04-15
3
Medium Priority
?
279 Views
Last Modified: 2008-03-06
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.
0
Comment
Question by:ba272
  • 2
3 Comments
 
LVL 23

Accepted Solution

by:
b1xml2 earned 2000 total points
ID: 13794050
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
 
LVL 23

Expert Comment

by:b1xml2
ID: 13794493
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
 

Author Comment

by:ba272
ID: 13795882
Thanks.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question