時間:分:秒の形式で時間を比較する次のプログラムがあります。
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;
}
そして主に…
sort(TimeArray, TimeArray + NumberOfTimes, CompareTimes);
ただし、これは正しくソートされていないようです。一方、CompareTimes メソッドを次のように変更すると:
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;
}
その後、すべてが正常に機能します。2 番目の入力が最初の入力より大きい場合、sort 関数は true を返す必要があると思いました。最初のケースでは機能しなかったのに、2 番目のケースでは機能したのはなぜですか?