I am looping through an XmlNodeList
and getting the date of the nodes creation in a string[]. For example:
date[0] = 2016 //year
date[1] = 07 //month
date[2] = 23 //day
My question is, what would be the most efficient way of comparing this string[]
to another string[]
, to find the which one has the latest date? I could do it with a bunch of if statements comparing each element to one another, but feel that isn't the best/prettiest solution. Thanks in advance.
You could use ParseExact
to create a DateTime
out of the string[]
. Then order by it:
List<string[]> data = new List<string[]>
{
new string[] { "2016", "07", "23" },
new string[] { "2017", "01", "01" }
};
var latestTime = data.OrderByDescending(item =>
DateTime.ParseExact(string.Join("/", item), @"yyyy/MM/dd", null))
.FirstOrDefault();
//latestTime = 1/1/2017