Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Sort an array of string of dates in ascending order

  • Difficulty Level : Easy
  • Last Updated : 03 Nov, 2021

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:
 

CPP




// 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);
}

Python3




# Python3 program to sort the dates in a string array
 
# Map to store the numeric value of each month depending on
# its occurrence i.e. Jan = 1, Feb = 2 and so on.
monthsMap=dict()
 
# Function which initializes the monthsMap
def 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
 
def cmp(date):
    date=date.split()
    return int(date[2]),monthsMap[date[1]],int(date[0]),
 
 
# Utility function to print the contents
# of the array
def printDates(dates, n):
    for i in range(n):
        print(dates[i])
 
# Driver code
if __name__ == '__main__':
 
    dates = [ "24 Jul 2017", "25 Jul 2017", "11 Jun 1996",
                       "01 Jan 2019", "12 Aug 2005", "01 Jan 1997" ]
    n = len(dates)
 
    # Order the months
    sort_months()
 
    # Sort the dates
    dates.sort(key=cmp)
 
    # Print the sorted dates
    printDates(dates, n)
 
# This code is contributed by Amartya Ghosh
Output: 
11 Jun 1996
01 Jan 1997
12 Aug 2005
24 Jul 2017
25 Jul 2017
01 Jan 2019

 

Time Complexity: O(N * log(N))  
Auxiliary Space: O(1)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!