Sort an Array of dates in ascending order using Custom Comparator | C # implementation to sort the array of dates in the form of " DD - MM - YYYY " using custom comparator ; Comparator to sort the array of dates ; Condition to check the year ; Condition to check the month ; Condition to check the day ; Function that prints the dates in ascensding order ; Sort the dates using library sort function with custom Comparator ; Loop to print the dates ; Driver Code
using System ; using System . Collections . Generic ; using System . Linq ; class GFG { static int myCompare ( string date1 , string date2 ) { string day1 = date1 . Substring ( 0 , 2 ) ; string month1 = date1 . Substring ( 3 , 2 ) ; string year1 = date1 . Substring ( 6 , 4 ) ; string day2 = date2 . Substring ( 0 , 2 ) ; string month2 = date2 . Substring ( 3 , 2 ) ; string year2 = date2 . Substring ( 6 , 4 ) ; return string . Compare ( year1 , year2 ) ; return string . Compare ( month1 , month2 ) ; return string . Compare ( day1 , day2 ) ; } static void printDatesAscending ( List < string > arr ) { arr . Sort ( myCompare ) ; for ( int i = 0 ; i < arr . Count ; i ++ ) Console . WriteLine ( arr [ i ] ) ; } static public void Main ( ) { List < string > arr = new List < string > ( ) ; arr . Add ( "25-08-1996" ) ; arr . Add ( "03-08-1970" ) ; arr . Add ( "09-04-1994" ) ; arr . Add ( "29-08-1996" ) ; arr . Add ( "14-02-1972" ) ; printDatesAscending ( arr ) ; } }