DirectoryInfo sort by LastWriteTime

I am using DirectoryInfo to populate a Repeater control.  I have found an example of how to sort a DataGrid but not a Repeater.  

How do I sort a Repeater on LastWriteTime whose data comes from DirectoryInfo?
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
   Dim DiectorySupplements As New DirectoryInfo(Server.MapPath("archives/supplements"))
   Supplements.DataSource = DiectorySupplements.GetFiles("*.pdf")
   Supplements.DataBind()
End Sub


<asp:Repeater ID="Supplements" runat="server">
  <ItemTemplate>
    <asp:Label ID="Label1" runat="server"  Text='<%# Eval("Name") %>' />
  </ItemTemplate>
</asp:Repeater>

Open in new window

cgcmqAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

pschramaCommented:
Example using Linq (vb version), you need to add System.Linq namespace for this.

DiectorySupplements.GetFiles("*.pdf").OrderBy(Function(fi) fi.LastWriteTime)
0
Alpesh PatelCommented:
Imports System.IO

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 to use it…

 

Dim dirinfo As DirectoryInfo

Dim allFiles() As FileInfo

 

dirinfo = New DirectoryInfo(sSelPath)

allFiles = dirinfo.GetFiles("*.xml")

Array.Sort(allFiles, New clsCompareFileInfo)

For Each fl As FileInfo In allFiles

    MessageBox(fl.FullName.ToString())

Next

 
REF:http://geekswithblogs.net/ntsmith/archive/2006/08/17/88250.aspx
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cgcmqAuthor Commented:
I edited my code as follows:

<%@ Import Namespace="System.Linq" %>

Dim DiectorySupplements As New DirectoryInfo(Server.MapPath("archives/supplements"))
Supplements.DataSource = DiectorySupplements.GetFiles("*.pdf").OrderBy(Function(fi) fi.LastWriteTime)
Supplements.DataBind()

Which returns the following error:
Compiler Error Message: BC30456: 'OrderBy' is not a member of 'System.Array'.
0
cgcmqAuthor Commented:
Worked like a charm.  I altered one line in order to sort the files in descending order:

Compare = DateTime.Compare(File2.LastWriteTime, File1.LastWriteTime)

Cheers!
0
pschramaCommented:
You need .NET Framework 3.5 to use the System.Linq namespace.

Otherwise you can use the IComparer approach, or adapt this example:


protected void Page_Load(object sender, EventArgs e)
      {
         DirectoryInfo dirInfo = new DirectoryInfo(@"C:\Temp");

         FileInfo[] fileInfoArray = dirInfo.GetFiles("*.*");
         Array.Sort(fileInfoArray, CompareFiles);

         foreach (FileInfo fi in fileInfoArray)
         {
            Response.Write(fi.FullName + "<br />");
         }
      }

      private int CompareFiles(FileInfo fi1, FileInfo fi2)
      {
         return fi1.LastWriteTime.CompareTo(fi2.LastWriteTime);
      }

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.