0

I have some code that outputs data from each array, but I was just wondering for my twoData array is there a way so that the user can see the dates in either ascending or descending order. At the moment it outputs like:

16/03/2015 13/02/2015 12/02/2015 03/02/2015 02/02/2015 30/01/2015 29/01/2014 28/01/2014 27/01/2014 26/01/2013 23/01/2013 22/01/2013

Thanks.

static void Main(string[] args)
    {
        //int ctr = 0; 

        string[] oneData = File.ReadAllLines("One.txt");
        string[] twoData = File.ReadAllLines("Two.txt");

         Console.WriteLine("Which array would you like to view?");
         string input = Console.ReadLine(); 

            Console.Write("\n");

          if (input.ToLower() == "one")
             Console.Write(string.Join("\n", oneData));

          else if(input.ToLower() == "two")
             Console.Write(string.Join("\n", twoData));
    }

2 Answers 2

4

To sort the strings in date order you would parse the values to dates:

dateData = dateData
  .OrderBy(d => DateTime.ParseExact(d, "dd'/'MM'/'yyyy", CultureInfo.InvariantCulture))
  .ToArray();

Use OrderByDescending for the reverse order.

Sign up to request clarification or add additional context in comments.

Comments

0
 internal class HiringDate :IComparable
{
    public int day { get; set; }
    public int month { get; set; }
    public int year { get; set; }

  



    public HiringDate(int day=1, int month=1, int year=2000)
    {
        this.day = day;
        this.month = month;
        this.year = year;
    }

    public override string ToString()
    {
        return $"{day}-{month}-{year}";
    }

    public int CompareTo(object obj)
    {
        HiringDate hD = obj as HiringDate;
        if (year.CompareTo(hD.year) == 0)
        {
            if (month.CompareTo(hD.month) == 0)
            {
               return day.CompareTo(hD.day);    

            }
            else return month.CompareTo(hD.month);


        }
        else return year.CompareTo(hD.year);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.