Click here to Skip to main content
15,888,401 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i have belew code and i want sort it by file date desc(fileArr) how?
i don't know how to use OrderBy function in this code please modify for me.
C#
string path = Server.MapPath(".") + "\\Upload\\VideoGallery";

DirectoryInfo di = new DirectoryInfo(path);
FileInfo[] fileArr = di.GetFiles("*.flv");

thanks.
Posted

You can easily do it using Linq. Try this:
C#
Array.Sort(fileArr, (f1, f2) => f1.LastWriteTime.CompareTo(f2.LastWriteTime));

Or:
C#
var sortedFiles = new DirectoryInfo(path).GetFiles()
                              .OrderBy(f => f.LastWriteTime)
                              .ToList();



--Amit
 
Share this answer
 
Comments
CPallini 18-Jun-13 5:00am    
5.
_Amy 18-Jun-13 5:05am    
Thank You. :)
Behnam Mohammadi 18-Jun-13 8:47am    
_Amy thank you for you'r solution but if i have
galleryCat = System.IO.Directory.GetDirectories(Server.MapPath(".") + "/Upload/ImageGallery"); how to order by date created DESC?
_Amy 19-Jun-13 0:06am    
Order it by desc. Try this:
Array.Sort(fileArr, (f1, f2) => f1.LastWriteTime.CompareTo(f2.LastWriteTime));
var files = fileArr.OrderByDescending(s => s.LastWriteTime);//If you want in desc order
foreach (FileInfo f in files)
{
Console.WriteLine(f.Name + "\t" + f.LastWriteTime);
}

--Amit
Behnam Mohammadi 18-Jun-13 8:56am    
and what is f?
This Stack Overflow question might interest you: "Sorting Files by date"[^] (see the top answer).
 
Share this answer
 
Comments
Thanks7872 18-Jun-13 4:45am    
Wow...Exxxxxxcellent shot...! Good one..I was also confused lil bit about the same....Clearly needs to be upvoted.
CPallini 18-Jun-13 5:00am    
Thank you.
_Amy 18-Jun-13 5:06am    
+5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900