Sort an Array of dates in ascending order using Custom Comparator

Given an array arr[] of N dates in the form of “DD-MM-YYYY”, the task is to sort these dates in ascending order.

Examples:

Input: arr[] = { “25-08-1996”, “03-08-1970”, “09-04-1994” }
Output:
03-08-1970
09-04-1994
25-08-1996

Input: arr[] = { “03-08-1970”, “09-04-2020”, “19-04-2019″”}
Output:
03-08-1970
19-04-2019
09-04-2020

Approach:



  1. Create a Custom comparator function that compares two dates as below:
    • First compare the year of the two elements. The element with greater year will come after the other element.
    • If the year of both the dates is same then compare the months. The element with a greater month will come after the other element.
    • If the month of both the dates is same then compare the dates. The element with greater date will come after the other element.
  2. Then sort the array using the defined custom comparator. In C++, it is done as:

    sort(initial position, ending position, comparator)

  3. Print the modified array.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation to sort the
// array of dates in the form of
// "DD-MM-YYYY" using custom comparator
  
#include <bits/stdc++.h>
using namespace std;
  
// Comparator to sort the array of dates
int myCompare(string date1,
              string date2)
{
    string day1 = date1.substr(0, 2);
    string month1 = date1.substr(3, 2);
    string year1 = date1.substr(6, 4);
  
    string day2 = date2.substr(0, 2);
    string month2 = date2.substr(3, 2);
    string year2 = date2.substr(6, 4);
  
    // Condition to check the year
    if (year1 < year2)
        return 1;
    if (year1 > year2)
        return 0;
  
    // Condition to check the month
    if (month1 < month2)
        return 1;
    if (month1 > month2)
        return 0;
  
    // Condition to check the day
    if (day1 < day2)
        return 1;
    if (day1 > day2)
        return 0;
}
  
// Function that prints the
// dates in ascensding order
void printDatesAscending(
    vector<string> arr)
{
    // Sort the dates using library
    // sort function with custom Comparator
    sort(arr.begin(), arr.end(), myCompare);
  
    // Loop to print the dates
    for (int i = 0; i < arr.size(); i++)
        cout << arr[i] << "\n";
}
  
// Driver Code
int main()
{
    vector<string> arr;
    arr.push_back("25-08-1996");
    arr.push_back("03-08-1970");
    arr.push_back("09-04-1994");
    arr.push_back("29-08-1996");
    arr.push_back("14-02-1972");
  
    printDatesAscending(arr);
  
    return 0;
}

chevron_right


Output:

03-08-1970
14-02-1972
09-04-1994
25-08-1996
29-08-1996

Time Complexity: O(N*log N)

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.