#include <iostream>
#include <vector>
#include <algorithm> // sort
#include <random> // mt19937 // random_device
#include <iomanip> // put_time
// Function to generate a random date
std::tm generateRandomDate(std::mt19937 &rng) {
std::uniform_int_distribution<int> yearDist(2000, 2025);
std::uniform_int_distribution<int> monthDist(1, 12);
std::uniform_int_distribution<int> dayDist(1, 28); // Simplified to avoid month length issues
std::tm date = {};
date.tm_year = yearDist(rng) - 1900; // tm_year is years since 1900
date.tm_mon = monthDist(rng) - 1; // tm_mon is 0-11
date.tm_mday = dayDist(rng);
return date;
}
// Function to print a date
void printDate(const std::tm &date) {
std::cout << std::put_time(&date, "%Y-%m-%d") << std::endl;
}
// Comparator for sorting dates
bool compareDates(const std::tm &a, const std::tm &b) {
return std::mktime(const_cast<std::tm*>(&a)) < std::mktime(const_cast<std::tm*>(&b));
}
int main() {
int numberOfDates = 10;
// Seed for random number generation
std::random_device rd;
std::mt19937 rng(rd());
// Generate a list of random dates
std::vector<std::tm> dates;
for (int i = 0; i < numberOfDates; i++) {
dates.push_back(generateRandomDate(rng));
}
// Print unsorted dates
std::cout << "Unsorted Dates:" << std::endl;
for (const auto &date : dates) {
printDate(date);
}
// Sort the dates
std::sort(dates.begin(), dates.end(), compareDates);
std::cout << "\nSorted Dates:" << std::endl;
for (const auto &date : dates) {
printDate(date);
}
}
/*
run:
Unsorted Dates:
2014-11-22
2012-11-22
2023-08-14
2000-07-11
2019-07-11
2011-06-27
2015-01-11
2014-03-20
2001-08-28
2011-07-11
Sorted Dates:
2000-07-11
2001-08-28
2011-06-27
2011-07-11
2012-11-22
2014-03-20
2014-11-22
2015-01-11
2019-07-11
2023-08-14
*/