This question is asked in the Programming Test Round in Nagarro. This question firstly seems quiet simple but it is a little tricky. The requirements are – date should be in string, date should be in format dd/MM/yyyy, the output will sort the date in ascending order moreover there should be NO zero appended with single digits. Like if your date is 05/09/2017 then it should show like 5/9/2017 in output.
Before Sorting { “27-01-2017”, “01-02-2011”, “3-8-2018”, “9-9-2010” }
using System; using System.Linq; namespace Nagarro { class Program { static void Main(string[] args) { DateTime dt; string[] dates = new string[] { "27-01-2017", "01-02-2011", "3-8-2018", "9-9-2010" }; Console.WriteLine("Sorted Dates in Ascending Order"); var orderedDates = dates.OrderBy(x => DateTime.Parse(x)).ToList(); // For Descending , use OrderByDescending foreach (string item in orderedDates) { dt = Convert.ToDateTime(item); Console.WriteLine(dt.ToString("M-d-yyyy")); } Console.ReadKey(); } } }