0

I have a list of strings that contains paths to files. I want to order this list according to the creation time of the files (first element should be the newest file) I work with C#. I thought of using a lambda but so far I get stuck with this line. it recognizes that result as DateTime object, how can I get the name of the file itself. It feels like a simple problem but I am new to lambda expression.

nameList.Sort(x => File.GetCreationTime(x));

thanks,

EricD1990
  • 33
  • 1
  • 5
  • So you want two things - you want to 1) sort the list according to creation timestamp, and then 2) convert the resulting list of paths to just the file names, is that correctly understood? – Mathias R. Jessen Apr 09 '21 at 17:52
  • 1/ is correct and 2/ i just want that original list reordered that's all. the original strings... – EricD1990 Apr 09 '21 at 17:54
  • You already have that. Try inspecting `nameList` after calling `Sort(...)` – Mathias R. Jessen Apr 09 '21 at 17:57
  • i forgot the error message: Cannot convert lambda expression to type 'IComparer' because it is not a delegate type – EricD1990 Apr 09 '21 at 17:58
  • Does this answer your question? [IComparer using Lambda Expression](https://stackoverflow.com/questions/2893176/icomparer-using-lambda-expression) – Mathias R. Jessen Apr 09 '21 at 18:02
  • Or `var fileList = new DirectoryInfo([Your Path]).EnumerateFiles().OrderBy(f => f.CreationTime);`, so you have more options. – Jimi Apr 09 '21 at 18:07
  • @Jimi your solution seems nice but i get the following error. 'IEnumerable' does not contain a definition for 'OrderBy' and no accessible extension method 'OrderBy' accepting a first argument of type 'IEnumerable' could be found (are you missing a using directive or an assembly reference? – EricD1990 Apr 09 '21 at 18:19
  • You need `using System.Linq;`. -- When you have this kind of notifications, just put the caret inside the underlined keyword and press `ALT+ENTER`, VS will suggest the namespace / assembly / NuGet Package to add. – Jimi Apr 09 '21 at 18:21
  • thank you so much; i adopt this solution!! – EricD1990 Apr 09 '21 at 18:24
  • Well, if you find that this is a solution to a problem, then post an answer with a description that explains why this solves the problem. -- BTW, having a collection of `FileInfo` objects is quite good for data binding: you can handle different *situations* because you have a number of properties to act upon. -- Remember to call `Refresh()` before accessing a `FileInfo` object (to refresh the cache, so you don't try to access a file that has been modified / moved / deleted etc). – Jimi Apr 09 '21 at 18:29
  • So duplicate of [Order (and enumerate) directory listing by file creation date?](https://stackoverflow.com/q/10965165/3744182) then? – dbc Apr 09 '21 at 18:32

1 Answers1

0

Answer of Jimi is the easiest one:

var fileList = new DirectoryInfo([Your Path]).EnumerateFiles().OrderBy(f => f.CreationTime);

it's easy to work with fileinfo

EricD1990
  • 33
  • 1
  • 5