0
votes

Sort a list of dates in ascending order given the data format shown below: Each date is in the form dd mmm yyyy where: dd is in the set {0-31} mmm is in the set {Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec} yyyy is four digits Example dates = ['01 Mar 2017','03 Feb 2017','15 Jan 1998'] The array dates sorts to to ['15 Jan 1998' '03 Feb 2017' '01 Mar 2017']

Function Description : Complete the function sortDates in the editor below. sortDates has the following parameter: string dates[n]: an array of strings, each field separated by a space Returns: string arr[n]: The function must return an array of date strings sorted chronologically ascending

Constraints 2≤lengthofdates≤30

Thank Please complete the sortdates function :

Class Results{ Public static List SortDates(List dates) }

1
The solution is almost always: parse the dates and sort them.Llama
"Thank Please complete the sortdates function" - Huh?! Stack Overflow isn't a code-writing service. Resources: 1. How to parse date - Just remove the HH:mm part since you don't have a time component. 2. How to sort a date. -- If you get stuck, feel free to revise your question with a minimal reproducible example and explanation of where you got stuck.Llama

1 Answers

0
votes

Here is the solution

using System;
using System.Collections.Generic;
using System.Linq;

namespace Results
{
    class Program
    {
        static void Main(string[] args)
        {
            var dates = new string[] { "01 Mar 2017", "03 Feb 2017", "15 Jan 1998" };
            var sortedDates =SortDate(dates);
            Console.WriteLine(String.Join(',', sortedDates));
        }

        private static IEnumerable<string> SortDate(string[] dates)
        {
            return dates.Select(x => Convert.ToDateTime(x)).OrderBy(y => y)
                .Select(z => z.ToString("dd MMM yyyy"));
        }
    }
}