I have a List with dates.
My list is:
{\"01/01/2013\",\"10/01/2013\",\"20/01/2013\"}
I want to sort the list
To convert DateTime string of custom format to string of unified format, you can adopt following simple and safe snippet.
string formatStr = "yyyyMMddHHmmss";
string result = Convert.ToDateTime(inputStr).ToString(formatStr);
Inside the code, formatStr can be any possible forms the DateTime class can accept, you can set it as the format you need, here you can just use dd/MM/yyyy which is matched with your target format string such as "20/01/2013".
So for your case, the code can be simple as below:
List list = new List { "01/01/2013", "10/01/2013", "20/01/2013" };
var listAfterSorting = list.OrderByDescending(t =>
Convert.ToDateTime(t).ToString("dd/MM/yyyy")
.ToList());
While in some cases, using ParseExact to parse a data time string, it will throws error/exception String was not recognized as a valid DateTime, in that case if you turn to use TryParseExact, the result is probably default(DateTime)= 1/1/0001 12:00:00 AM because of parsing failed. Therefore, I do not recommend ParseExact or TryParseExact.