Skip to content

Instantly share code, notes, and snippets.

@minorsecond
Last active June 17, 2021 23:02
Show Gist options
  • Save minorsecond/97f196e7b37a1543cf77e80a2e306d4b to your computer and use it in GitHub Desktop.
Save minorsecond/97f196e7b37a1543cf77e80a2e306d4b to your computer and use it in GitHub Desktop.
date sorter
bool Database::compare_date(const Drink &a, const Drink &b) {
/*
* Determine if second date is greater than the first date.
* @param a: Date from a struct in YYYY-MM-DD format
* @param b: Date from b struct in YYYY-MM-DD format
* @return: True if second date is more recent than the first date. Else, false.
*/
int a_year = std::stoi(a.date.substr(0, 4));
int a_month = std::stoi(a.date.substr(5, 7));
int a_day = std::stoi(a.date.substr(8, 9));
int b_year = std::stoi(b.date.substr(0, 3));
int b_month = std::stoi(b.date.substr(5, 6));
int b_day = std::stoi(b.date.substr(8, 9));
std::cout << "Year: " << a_year << " Month: " << a_month << " Day: " << a_day << std::endl;
std::cout << "A Full date: " << a.date << std::endl;
std::cout << "B Full date: " << b.date << std::endl;
std::cout << "A PK: " << a.id << " B PK: " << b.id << std::endl;
if (a_year <= b_year) {
if (a_month <= b_month) {
if (a_day <= b_day) {
if (a.id < b.id) {
std::cout << "B is greater than A" << std::endl;
return true;
}
}
}
}
// Else:
std::cout << "B is less than A" << std::endl;
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment