Search code examples
javaarraylistrotationjava-timedayofweek

Order an ArrayList<DayOfWeek> based on a specific value


I have an ArrayList<DayOfWeek> let's say ["MONDAY","WEDNESDAY","FRIDAY"]

My aim is to order it based on a DayOfTheWeek in a circular way.

Input: "WEDNESDAY"

SortedList: ["WEDNESDAY","FRIDAY","MONDAY"]

The list is guaranteed to be sorted from Monday through Sunday. The input day is not guaranteed to be in the list, in this case the sorting starts from the next value present in the list.


Solution

  • I would recommend using a method similar to this:

    how to order Arrays value as circular format in java?

    Where each DayOfWeek has its own ID number corresponding to its intended position in ordering, so you can rotate around those numbers.