Re: Getting Direcotry files sorted by date, is it possible?

From: Jonathan Stowe (jns_at_gellyfish.com)
Date: 02/16/05


Date: Wed, 16 Feb 2005 15:12:30 +0000

Raed Sawalha <RaedSawalha@discussions.microsoft.com> wrote:
> I wondering if it possilble to get the directory files ordered by creation date
>
> string [] files = System.IO.Directory.GetFiles(strPath,"*.msg")
>
> can I sort the returned file by creation date?
>

You need to implement your own IComparer do the specialized sort:

using System;
using System.IO;
using System.Collections;
                                                                                
public class SortFiles
{
   public class CompareFileByDate :IComparer
   {
      int IComparer.Compare(Object a, Object b)
      {
         FileInfo fia = new FileInfo((string)a);
         FileInfo fib = new FileInfo((string)b);
                                                                                
         DateTime cta = fia.CreationTime;
         DateTime ctb = fib.CreationTime;
                                                                                
         return DateTime.Compare(cta, ctb);
      }
   }
                                                                                
   public static void Main()
   {
      string [] files = System.IO.Directory.GetFiles("..");
                                                                                
      IComparer fileComparer = new CompareFileByDate();
      Array.Sort(files, fileComparer);
                                                                                
      foreach ( string f in files )
      {
         Console.WriteLine(f);
      }
                                                                                
   }
}

/J\



Relevant Pages

  • Re: The Paradox of Speciation
    ... > I take evolution of some sort as a given. ... which is the Creation Museum's metaphor in opposition to the ... Of course, there's also the Tinkerer variation, whereby evolution ... weakness of your sinking position. ...
    (talk.origins)
  • Re: Sorting on Multiple Columns
    ... > sort a data set by multiple columns. ... public class Student ... public Student(final int theStudentId, final String theFirstName, final ... public class TryStudent ...
    (comp.lang.java.programmer)
  • Re: Creation Model (was: The correct definition.)
    ... If the first creation was the Cambrian and the third was ... I'm curious what it is like when a species ... few thousand "kinds" that were taken onto the ark evloved into the ... Some sort of typical individuals of that kind are that kind. ...
    (talk.origins)
  • Re: Maybe Safari 4 actually is for Windows
    ... You get a lot of capability related to the creation of complex ... Google Docs works ... it works *better* for this sort ... because of the low-overhead sharing and precisely because ...
    (comp.sys.mac.advocacy)
  • Re: Atheist bus seen as offensive
    ... Gareth McCaughan wrote: ... "the physical world that we inhabit" or something of the sort, ... and believes in a spiritual world that existed before the creation ...
    (uk.religion.christian)