11

Using this example off MSDN:

using System.Collections.Generic;  
using System.IO;  

namespace CollectionTest  
{  
    public class ListSort  
    {  
        static void Main(string[] args)  
        {  
            List<FileInfo> files = new List<FileInfo>();  
            files.Add(new FileInfo("d(1)"));  
            files.Add(new FileInfo("d"));              
            files.Add(new FileInfo("d(2)"));  

            files.Sort(new CompareFileInfoEntries());  
        }           

    }  

    public class CompareFileInfoEntries : IComparer<FileInfo> 
    {  
        public int Compare(FileInfo f1, FileInfo f2)  
        {  
            return (string.Compare(f1.Name, f2.Name));  
        }  
    }  

}  

How would I compare the date of creation.

F1 has a property "creation" date which is a FileSystemInfo.Datetime, yet when I try this:

  public class CompareFileInfoEntries : IComparer<FileInfo>
  {
      public int Compare(FileInfo f1, FileInfo f2)
      {

          return (DateTime.Compare(DateTime.Parse(f1.CreationTime), f2.CreationTime));
      }
  }  
}

I get overload method matches for String. compare(string,string) Note: Ive used two methods in the above script to attempt returning the creation time. Neither have worked - they would both be the same in my actual script.

CLosest I can get is:

return (DateTime.Compare(DateTime.Parse(f1.CreationTime.ToString()), DateTime.Parse(f2.CreationTime.ToString() )));
JustAnotherDeveloper
  • 3,167
  • 11
  • 37
  • 68

4 Answers4

25

Description

You can simple use LINQ (namespace System.Linq) for that.

Language Integrated Query (LINQ, pronounced "link") is a Microsoft .NET Framework component that adds native data querying capabilities to .NET languages

Sample

List<FileInfo> orderedList = files.OrderBy(x => x.CreationTime).ToList();

More Information

dknaack
  • 60,192
  • 27
  • 155
  • 202
4

Umm what about using linq

files.OrderBy(f=>f.CreationTime)
undefined
  • 33,537
  • 22
  • 129
  • 198
0
Dim filePath as string = "c:\\"

This command get directory file list ordered by ASC

Dim orderedFiles = New System.IO.DirectoryInfo(filePath).GetFiles("*.xml")
                                                        .OrderBy(Function(x) x.CreationTime)

This command get directory file list ordered by DESC

Dim orderedFiles = New System.IO.DirectoryInfo(filePath).GetFiles("*.xml")
                                                        .OrderByDescending(Function(x) x.CreationTime)
icebat
  • 4,696
  • 4
  • 22
  • 36
-2

Try this:

public class CompareFileInfoEntries : IComparer<FileInfo> 
    { 
        public int Compare(FileInfo f1, FileInfo f2) 
        {
            return (string.Compare(f1.CreationTime.ToString(), f2.CreationTime.ToString())); 
        } 
    } 
Tapas Mahata
  • 343
  • 6
  • 22
  • That is almost guaranteed to fail to work properly, though it might *purely accidentally* work on some Cultures. – Andrew Barber Jan 25 '12 at 16:26
  • @Barber - Even if the cultures changes, both of the fileinfo creation time will be changed against that culture and if both of them are changed accordingly then it should work fine.. I am confused !!! – Tapas Mahata Jan 27 '12 at 05:14
  • In en-us, the date would outpit like this: *1/28/2012 12:28:30pm* compared to *5/15/1972 5:34:10pm* The sorting would be *wrong*. You are converting to a string which does *not* sort the same way as a date. – Andrew Barber Jan 27 '12 at 05:30