Reputation: 45
I've problem with sorting files by date; this's my sample code :
string[] files = new string {"file1.txt", "file3.txt", "file4.txt"}
var list = new List<string> {"10/03/2015", "01/01/2015", "20/08/2015"};
list.Sort((a, b) => a.CompareTo(b));
I wanna get in the output all files sorting by date without losing the order between each file and its time:
// file3.txt 01/01/2015
// file1.txt 10/03/2015
// file4.txt 20/08/2015
So my question is how can I according files to sort of date, Thanks :)
Upvotes: 0
Views: 992
Reputation: 120420
Easily done with Linq. First, Zip together your two collections and parse the dates:
var filesAndDates = files
.Zip(list,
(filename,dateString) => new{filename,
date = DateTime.ParseExact(
dateString,
"dd/MM/yyyy",
CultureInfo.InvariantCulture)});
then order them:
var orderedFilesAndDates = filesAndDates.OrderBy(x => x.date);
I find myself wondering how you ended with 2 "synchronized" lists of stuff. You could make a composite type and keep them in a single list. It's far easier to manage that way...
Upvotes: 1
Reputation: 13
first you need to parse the values to DateTime type and then do sort.
Upvotes: 0