• Client login
  • Work for us

All the latest from the team...

Tuesday, 2 December 2008

ASP.net C# ordering files returned by GetFiles

A very quick post today to share a bit of a moan about ASP.net and the Directory.GetFiles Method which most of you will no doubt know only returns a list of files order alphabetically. Now whilst we've been developing a system for a client we've had the need to order by date last modified - which I'm sure is a common request for many of you reading this. Well with no further ado here is a very simple class so you can sort the array returned by the GetFiles method...

public class FilesDescendingComparer : IComparer
{
#region IComparer Members

public int Compare(object x, object y)
{
return DateTime.Compare(((FileInfo)y).CreationTime,((FileInfo)x).CreationTime);
}

#endregion
}

To invoke this method is very simple too...

Array.Sort(theFiles, new FilesDescendingComparer());

Where theFiles is the result of your GetFiles call.

Happy coding!

Labels: , , , , , , ,

0 Comments:

Post a Comment

<< Home

RSS feed ATOM feed Add to Technorati Favorites View Jon Harvey's profile on LinkedIn