views:

719

answers:

4

I don't think the, System.Collections.ObjectModel has any sort or order by capability.

I have a list of files and I'd like to sort by the file date.

Dim list AS System.Collections.ObjectModel.ReadOnlyCollection(Of String)
list = My.Computer.FileSystem.GetFiles("C:\SearchFolder" _ 
    , FileIO.SearchOption.SearchByTopLevelOnly _
    , "TheFileName*.txt")

Dim sTheLastFile AS String

sTheLastFile = list.Max()

This returns the last file, but based on file name and not date. I think I need to add .OrderBy(... just can't get that part.

+1  A: 
using System.IO;

public static void Main()
{
 DirectoryInfo di = new DirectoryInfo("c:\\temp\\");
 FileSystemInfo[] files = di.GetFileSystemInfos("*.mp3");
 printFiles(files);

 Array.Sort(files, CompareFileByDate);
 printFiles(files);

}

public static int CompareFileByDate(FileSystemInfo f1, FileSystemInfo f2)
{
 return DateTime.Compare(f1.LastWriteTime, f2.LastWriteTime);
}

public static void printFiles(FileSystemInfo[] files)
{
 foreach(FileSystemInfo file in files)
 {
  Console.WriteLine(file.Name);
 }
 Console.WriteLine("********************************");
}

See if this helps you at all.

I have used LastWriteTime property. You can choose whichever works for you (CreationTime or LastAccessTime).

EDIT: Sure, this can be converted to more compact syntax using c# 3.0 & support for lambda expressions.

EDIT2:

from file in new DirectoryInfo(@"c:\temp\").GetFileSystemInfos("*.mp3")
orderby file.LastWriteTime
select file

EDIT3: vb.net version of the above c# code

from file in new DirectoryInfo("c:\temp\").GetFileSystemInfos("*.mp3") _
order by file.LastWriteTime _
select file

EDIT4: Is this what you are looking for?
This will give you the max. date of the LastWriteTime of all *.mp3 files.

(from file in new DirectoryInfo("c:\temp\").GetFileSystemInfos("*.mp3") _
order by file.LastWriteTime descending _
select file.LastWriteTime).Take(1)

OR

(from file in new DirectoryInfo("c:\temp\").GetFileSystemInfos("*.mp3") _
select file.LastWriteTime).Max()
shahkalpesh
How would I get the order by file.LastWriteTime into my code?
Jeff O
Doesn't any of the code examples above help? Could you edit your question with what is expected, keeping in mind code example posted in this answer?
shahkalpesh
A: 

To accomplish this please try the following...

Setup a new class implementing the IComparer interface. This will be used to perform the comparisons. IComparer provides a way to customize the sort order of a collection. Note that the example below uses the LastWriteTime as the basis for comparison however this can be changed to whatever property you see fit.

Public Class clsCompareFileInfo
    Implements IComparer
    Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
        Dim File1 As FileInfo
        Dim File2 As FileInfo

        File1 = DirectCast(x, FileInfo)
        File2 = DirectCast(y, FileInfo)

        Compare = DateTime.Compare(File1.LastWriteTime, File2.LastWriteTime)
    End Function
End Class

Then, grab the file collection and perform the following actions to sort the files accordingly.

 Dim dirinfo As DirectoryInfo = New DirectoryInfo("C:\SearchFolder")
        Dim allFiles() As FileInfo = dirinfo.GetFiles("TheFileName*.txt", SearchOption.TopDirectoryOnly)
        Array.Sort(allFiles, New clsCompareFileInfo)
        For Each fl As FileInfo In allFiles
            MsgBox(fl.FullName.ToString())
        Next
Jakkwylde
Your first line is creating an error: Value of type 'System.Collections.ObjectModel.ReadOnlyCollection(Of String)' cannot be converted to '1-dimensional array of System.IO.FileInfo'
Jeff O
The code has been modified, please try the above
Jakkwylde
A: 

With this class you can order the files using the appropiate criteria (you must add private helper classes for every criteria you needed ;-) )


Imports System.IO

Class FilesTools
    Private Class HelperSortByLastWriteTimeAsc
        Implements IComparer(Of FileInfo)

        Public Function Compare(ByVal x As System.IO.FileInfo, _
                                ByVal y As System.IO.FileInfo) As Integer Implements System.Collections.Generic.IComparer(Of System.IO.FileInfo).Compare
            Return Date.Compare(x.LastWriteTime, y.LastWriteTime)
        End Function
    End Class

    Private Class HelperSortByLastWriteTimeDesc
        Implements IComparer(Of FileInfo)

        Public Function Compare(ByVal x As System.IO.FileInfo, _
                                ByVal y As System.IO.FileInfo) As Integer Implements System.Collections.Generic.IComparer(Of System.IO.FileInfo).Compare
            Return Date.Compare(y.LastWriteTime, x.LastWriteTime)
        End Function
    End Class

    Public Shared Function sortByLastTime() As IComparer(Of FileInfo)
        Return New HelperSortByLastWriteTimeAsc
    End Function

    Public Shared Function sortByLastTimeDesc() As IComparer(Of FileInfo)
        Return New HelperSortByLastWriteTimeDesc
    End Function

    Public Shared Function GetFilesSorted(ByVal path As String, _
                                          ByVal sort As IComparer(Of FileInfo)) As FileInfo()
        Dim info As FileInfo()
        info = New DirectoryInfo(path).GetFileSystemInfos()

        Array.Sort(Of FileInfo)(info, sort)

        Return info
    End Function

End Class
Alejandro Díaz
A: 

nothing to contrebiute , just to say thanks you it been very helpfull for me. Shmuel

shmuel