I want to sort these times in order:
4:05 AM
5:04 PM
6:04 AM
4:05 PM
5:04 AM
12:01 AM
12:01 PM
using Time class with
public int compareTo(Time t)
method.
if(this.getMeridians() != t.getMeridians())
return this.getMeridians().compareTo(t.getMeridians());
to sorted AM and PM, but I don't know how to sort hours and minutes. It's in 12-hour clock form, so 12:01 AM should be very first on the list. In order to do that, how should I fill up the compareTo(Time t)?
It should be like this.
12:01 AM
4:05 AM
5:04 AM
6:04 AM
12:01 PM
4:05 PM
5:04 PM
The answer by Guy is correct.
For fun I did the same kind of code but using the Joda-Time 2.3 library.
My code assumes you truly want times only, without dates. Therefore, you'll not get handling of Daylight Saving Time or other issues.
When run…
If you need the values output again as AM/PM format, search StackOverflow.com for examples of using formatters in Joda-Time to create strings ("print" method).