Sort an array of string of dates in ascending order

Given an array of strings dates[], the task is to sort these dates in ascending order.
Note: Each date is of the form dd mmm yyyy where:

  • Domain of dd is [0-31].
  • Domain of mmm is [Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec].
  • And, yyyy is a four digit integer.

Examples:

Input: dates[] = {“01 Mar 2015”, “11 Apr 1996”, “22 Oct 2007”}
Output:
11 Apr 1996
22 Oct 2007
01 Mar 2015



Input: dates[] = {“03 Jan 2018”, “02 Jan 2018”, “04 Jan 2017”}
Output:
04 Jan 2017
02 Jan 2018
03 Jan 2018

Approach: Extract the days, months and years as sub-strings from the string then compare two strings by years, if years for two dates are equal then compare their months. If months are also equal than the days will decide which date appears earlier in the calendar.

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to sort the dates in a string array
#include <bits/stdc++.h>
using namespace std;
  
// Map to store the numeric value of each month depending on
// its occurrence i.e. Jan = 1, Feb = 2 and so on.
unordered_map<string, int> monthsMap;
  
// Function which initializes the monthsMap
void sort_months()
{
    monthsMap["Jan"] = 1;
    monthsMap["Feb"] = 2;
    monthsMap["Mar"] = 3;
    monthsMap["Apr"] = 4;
    monthsMap["May"] = 5;
    monthsMap["Jun"] = 6;
    monthsMap["Jul"] = 7;
    monthsMap["Aug"] = 8;
    monthsMap["Sep"] = 9;
    monthsMap["Oct"] = 10;
    monthsMap["Nov"] = 11;
    monthsMap["Dec"] = 12;
}
  
// Comparator function to sort an array of dates
bool comp(string a, string b)
{
  
    // Comparing the years
    string str1 = a.substr(7, 5);
    string str2 = b.substr(7, 5);
    if (str1.compare(str2) != 0) {
        if (str1.compare(str2) < 0)
            return true;
        return false;
    }
  
    // Comparing the months
    string month_sub_a = a.substr(3, 3);
    string month_sub_b = b.substr(3, 3);
  
    // Taking numeric value of months from monthsMap
    int month_a = monthsMap[month_sub_a];
    int month_b = monthsMap[month_sub_b];
    if (month_a != month_b) {
        return month_a < month_b;
    }
  
    // Comparing the days
    string day_a = a.substr(0, 2);
    string day_b = b.substr(0, 2);
    if (day_a.compare(day_b) < 0)
        return true;
    return false;
}
  
// Utility function to print the contents
// of the array
void printDates(string dates[], int n)
{
    for (int i = 0; i < n; i++) {
        cout << dates[i] << endl;
    }
}
  
// Driver code
int main()
{
    string dates[] = { "24 Jul 2017", "25 Jul 2017", "11 Jun 1996",
                       "01 Jan 2019", "12 Aug 2005", "01 Jan 1997" };
    int n = sizeof(dates) / sizeof(dates[0]);
  
    // Order the months
    sort_months();
  
    // Sort the dates
    sort(dates, dates + n, comp);
  
    // Print the sorted dates
    printDates(dates, n);
}

chevron_right


Output:

11 Jun 1996
01 Jan 1997
12 Aug 2005
24 Jul 2017
25 Jul 2017
01 Jan 2019


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.