Something like this might work. It uses regex to extract the day name, and DayOfWeek
from the Java 8 Time API to get the day number from the day name.
List<String> dayList = new ArrayList<>(Arrays.asList(
"SATURDAY : C L O S E D",
"TUESDAY : 9:00AM - 5:00PM",
"FRIDAY : 9:00AM - 5:00PM",
"MONDAY : 9:00AM - 5:00PM",
"SUNDAY : C L O S E D",
"WEDNESDAY: 9:00AM - 5:00PM",
"APPOINTMENT REQUIREMENT",
"THURSDAY : 9:00AM - 5:00PM"));
// Sort list by day name
Pattern p = Pattern.compile("^(\\w+DAY)\\s*:");
dayList.sort(Comparator.<String>comparingInt(s -> {
Matcher m = p.matcher(s);
return (m.find() ? DayOfWeek.valueOf(m.group(1)).getValue() : 8);
}));
// Print the sorted list
dayList.forEach(System.out::println);
Output
MONDAY : 9:00AM - 5:00PM
TUESDAY : 9:00AM - 5:00PM
WEDNESDAY: 9:00AM - 5:00PM
THURSDAY : 9:00AM - 5:00PM
FRIDAY : 9:00AM - 5:00PM
SATURDAY : C L O S E D
SUNDAY : C L O S E D
APPOINTMENT REQUIREMENT
UPDATE Java 5+ solution, same output as above:
final class DayComparator implements Comparator<String> {
private final Pattern p = Pattern.compile(
"^(?:(MON)|(TUES)|(WEDNES)|(THURS)|(FRI)|(SATUR)|(SUN))DAY\\s*:");
public int compare(String s1, String s2) {
return dayValue(s1) - dayValue(s2);
}
private int dayValue(String s) {
Matcher m = this.p.matcher(s);
if (m.find())
for (int i = 1; ; i++)
if (m.start(i) != -1)
return i;
return 8;
}
}
List<String> dayList = new ArrayList<String>(Arrays.asList(
"SATURDAY : C L O S E D",
"TUESDAY : 9:00AM - 5:00PM",
"FRIDAY : 9:00AM - 5:00PM",
"MONDAY : 9:00AM - 5:00PM",
"SUNDAY : C L O S E D",
"WEDNESDAY: 9:00AM - 5:00PM",
"APPOINTMENT REQUIREMENT",
"THURSDAY : 9:00AM - 5:00PM"));
// Sort list by day name
Collections.sort(dayList, new DayComparator());
// Print the sorted list
for (String s : dayList)
System.out.println(s);