The other Answers here are good, but use outmoded classes.
java.time
With Java 8 and later we now have the java.time framework built-in (with back-ports for Java 6 & 7 and Android). A vast improvement over the old date-time classes.
The java.time classes include a pair of classes that fit your exact needs:
DayOfWeek
A handy enum to represent each of the seven days of the week, as defined by the ISO 8601 standard running from Monday to Sunday.
LocalTime
Represents a time-of-day without date and without time zone.
With these types pre-defined, you do not even need to define your own class as suggested by other comments and answers. At least if your definition of sorting the days runs Monday-Sunday. The DayOfWeek enum has predefined days of the weeks in that order. You could make your own class combining the DayOfWeek and LocalTime if it makes sense your greater project.
Java enums are really handy, flexible, and powerful (so learn more if they are new to you). Enums have their own special implementation of Set and Map, named appropriately, EnumSet and EnumMap. We can use an EnumMap to track each day of the week, mapping it to the time-of-day (a LocalTime object).
EnumMap<DayOfWeek , LocalTime> dayToTimeMap = new EnumMap<> ( DayOfWeek.class );
dayToTimeMap.put ( DayOfWeek.TUESDAY , LocalTime.parse ( "20:00" ) );
dayToTimeMap.put ( DayOfWeek.WEDNESDAY , LocalTime.of ( 15 , 0 ) );
dayToTimeMap.put ( DayOfWeek.THURSDAY , LocalTime.parse ( "20:00" ) );
dayToTimeMap.put ( DayOfWeek.FRIDAY , LocalTime.parse ( "18:00" ) );
dayToTimeMap.put ( DayOfWeek.SATURDAY , LocalTime.parse ( "15:00" ) );
Get the current day-of-week and time-of-day.
DayOfWeek today = DayOfWeek.WEDNESDAY;
LocalTime now = LocalTime.of ( 11 , 13 );
Make a pair of empty sets one to track the day-times that are the same or later than today-now, and one to track the earlier day-times. Being EnumSet, their natural order is the order declared in the DayOfWeek enum (Monday-Sunday, 1-7).
EnumSet<DayOfWeek> earlier = EnumSet.noneOf ( DayOfWeek.class );
EnumSet<DayOfWeek> later = EnumSet.noneOf ( DayOfWeek.class );
Loop the DayOfWeek-to-LocalTime map. See if the DayOfWeek is before, equal to, or later than today. If equal to today, the compare its LocalTime object to our now object. Assign this DayOfWeek object to either the earlier set or the later set.
for ( Map.Entry<DayOfWeek , LocalTime> entry : dayToTimeMap.entrySet () ) {
DayOfWeek key = entry.getKey ();
LocalTime value = entry.getValue ();
int comparison = key.compareTo ( today );
if ( comparison < 0 ) { // if earlier day…
earlier.add ( key );
} else if ( comparison == 0 ) { //If same day…
if ( value.isBefore ( now ) ) {
earlier.add ( key );
} else { // Else same time as now or later than now…
later.add ( key );
}
} else if ( comparison > 0 ) {
later.add ( key );
} else {
throw new RuntimeException ( "Unexpectedly reached IF-ELSE for comparison: " + comparison );
}
}
Dump to console. We want to loop the later set first, then the earlier set per the requirements laid out in the Question.
System.out.println ( "dayToStringMap: " + dayToTimeMap );
System.out.println ( "sorted by today: " + today + " " + now + " is: " );
for ( DayOfWeek dayOfWeek : later ) {
LocalTime localTime = dayToTimeMap.get ( dayOfWeek );
System.out.println ( dayOfWeek + " " + localTime );
}
for ( DayOfWeek dayOfWeek : earlier ) {
LocalTime localTime = dayToTimeMap.get ( dayOfWeek );
System.out.println ( dayOfWeek + " " + localTime );
}
When run.
dayToStringMap: {TUESDAY=20:00, WEDNESDAY=15:00, THURSDAY=20:00, FRIDAY=18:00, SATURDAY=15:00}
sorted by today: WEDNESDAY 11:13 is:
WEDNESDAY 15:00
THURSDAY 20:00
FRIDAY 18:00
SATURDAY 15:00
TUESDAY 20:00