How to sort an array of dates in C/C++?


Suppose we have an array of dates. Here we will see how to sort then using C or C++ code. The dates are stored in a class (struct can be used in C also). We will use the sort function of the C++ STL. For comparing dates, we have to write our own compare function that will be used in the sort function. Let us see the example to get better view.

Example

 Live Demo

#include<iostream>
#include<iostream>
#include<algorithm>
using namespace std;
class Date {
   public:
      int d, m, y;
};
bool compare(const Date &date1, const Date &date2){
   if (date1.y < date2.y)
      return true;
   if (date1.y == date2.y && date1.m < date2.m)
      return true;
   if (date1.y == date2.y && date1.m == date2.m && date1.d < date2.d)
      return true;
   return false;
}
void sortDateArray(Date arr[], int n) {
   sort(arr, arr+n, compare);
}
int main() {
   Date arr[] = {{20, 1, 2017},
   {25, 3, 2010},
   { 3, 12, 1956},
   {18, 10, 1982},
   {19, 4, 2011},
   { 9, 7, 2013}};
   int n = sizeof(arr)/sizeof(arr[0]);
   sortDateArray(arr, n);
   cout << "Sorted dates are" << endl;
   for (int i=0; i<n; i++) {
      cout << arr[i].d << " " << arr[i].m << " " << arr[i].y << endl;
   }
}

Output

Sorted dates are
3 12 1956
18 10 1982
25 3 2010
19 4 2011
9 7 2013
20 1 2017

Updated on: 03-Jan-2020

676 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements