Java – sort arrays by time

First, I use an array to get such information:

// Tuesday
array[2][1] = "tuesday";
array[2][2] = "20:00";

// Wednesday 
array[3][1] = "Wednesday";
array[3][2] = "15:00";

// Thursday 
array[4][1] = "Thursday";
array[4][2] = "20:00";

// Friday
array[5][1] = "Friday";
array[5][2] = "18:00";

// Saturday
array[6][1] = "Saturday";
array[6][2] = "15:00";

// Sunday
array[7][1] = "Sunday";
array[7][2] = "15:00";

How do I sort arrays by actual time and working days? Example: it's Wednesday – 11:13 The first array item will be array [3], then 4, 5, 6, 7, and then 2

thank you very much.

Solution

The other answers here are good, but use outdated classes

java. time

In Java 8 and later, we now have Java Time framework (with Java 6& 7 and Android back port) Compared with the old date time class, it is a great improvement

java. The time class contains a pair of classes that meet your exact needs:

>Dayofweeka convenience enum represents every day of seven days a week, defined by ISO 8601 standard, from Monday to Sunday. > Local time means time without date and time zone

With these types predefined, you don't even need to define your own classes as suggested by other comments and answers At least if you define date sorting from Monday to Sunday The DayOfWeek enumeration lists the number of days scheduled in this order If your larger project makes sense, you can build your own classes for DayOfWeek and Localtime

Java enums are very convenient, flexible and powerful (so learn more if they are not familiar with you) Enumeration has its own special implementation set and map, appropriately named enumset and enummap We can use enummap to track each day of the week and map it to time (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" ) );

Gets the current day of the week and time

DayOfWeek today = DayOfWeek.WEDNESDAY;
LocalTime Now = LocalTime.of ( 11,13 );

Make a pair of empty sets to track dates that are the same as or later than the present, and dates that track earlier dates As enumsets, their natural order is the order declared in the DayOfWeek enumeration (Monday to Sunday, 1-7)

EnumSet<DayOfWeek> earlier = EnumSet.noneOf ( DayOfWeek.class );
EnumSet<DayOfWeek> later = EnumSet.noneOf ( DayOfWeek.class );

Loop DayOfWeek to Localtime map Check whether DayOfWeek is before today, equal to or later than today If it is equal to today, compare its Localtime object with our current object Assign this DayOfWeek object to an earlier set or a 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 following set first, and then loop the previous set according to the requirements listed in the problem

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 running

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
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>