I have the following program to compare time in the form of Hours:minutes:seconds.
class time
{
public:
string Hours;
string Minutes;
string Seconds;
};
bool CompareTimes(time A, time B)
{
if (A.Hours < B.Hours)
{
return true;
}
if (A.Minutes < B.Minutes)
{
return true;
}
if (A.Seconds < B.Seconds)
{
return true;
}
return false;
}
And in the main...
sort(TimeArray, TimeArray + NumberOfTimes, CompareTimes);
However, this does not seem to sort it properly. On the other hand, if I change the CompareTimes method to the following:
bool CompareTimes(time A, time B)
{
if (A.Hours > B.Hours)
{
return false;
}
if (A.Minutes > B.Minutes)
{
return false;
}
if (A.Seconds > B.Seconds)
{
return false;
}
return true;
}
Then everything works properly. I thought the sort function needs to return true if the second input is greater than the first input. Why did it not work in the first case, but work in the second case?