Given two dates
a and
b, is_after(a, b) should be true
if and only if a is ordered-after
b.
For consistency with the standard library, let's write
is_before instead. Let's also rename it
operator<, operator less-than.
One date is less-than another if it occurred earlier in time:
1 2 3 4 5 6 7 8 9 10
|
bool operator<(Date a, Date b)
{
if (a.getYear() < b.getYear()) return true;
if (b.getYear() < a.getYear()) return false;
// years are equivalent; tiebreak using months
if (a.getMonth() < b.getMonth()) return true;
if (b.getMonth() < a.getMonth()) return false;
// months are equivalent; tiebreak using days
return a.getDay() < b.getDay();
}
|
As usual, you can call this function by name
operator<(first_date, second_date)
But you may also call it using the < sign:
first_date < second_date
Now inside
bubblesort, just use '
<' to compare dates. To compare in reverse order, note that b < a == a > b.
As is, your bubble-sort formulation will fail when passed a zero-size vector. Nothing's wrong with your algorithm as far as I can see - you're just being bitten by a design flaw in the standard library.
Problem is,
vec.size() is an
unsigned int. If its value is zero, subtracting one from it yields the largest possible value of
unsigned int.