根据日期顺序转换arraylist元素顺序

时间:2019-01-05 00:20:09

标签: java java-6

下面是数据库的输出。这些序列顺序不能每次都相同。这里的日子不是按顺序排列的。这些天我需要按顺序排列。

ArrayList dayList= [ MONDAY   : 9:00AM - 5:00PM , TUESDAY  : 9:00AM - 5:00PM , WEDNESDAY: 9:00AM - 5:00PM , FRIDAY   : 9:00AM - 5:00PM,  THURSDAY : 9:00AM - 5:00PM , SATURDAY : C L O S E D  , SUNDAY   : C L O S E D , APPOINTMENT REQUIREMENT]

我需要按如下顺序转换输出:

[ 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]

2 个答案:

答案 0 :(得分:1)

类似的事情可能起作用。它使用正则表达式提取日期名称,并使用Java 8 Time API中的DayOfWeek从日期名称中获取日期编号。

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);

输出

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

更新 Java 5+解决方案,与上述输出相同:

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);

答案 1 :(得分:0)

这在Java 6中不起作用,但是这个概念仍然存在并且很容易翻译:

Java 8流替代:

private static final List<String> DATES =
        List.of("MONDAY   : 9:00AM - 5:00PM",
                "TUESDAY  : 9:00AM - 5:00PM",
                "WEDNESDAY: 9:00AM - 5:00PM",
                "FRIDAY   : 9:00AM - 5:00PM",
                "THURSDAY : 9:00AM - 5:00PM",
                "SATURDAY : C L O S E D ",
                "SUNDAY   : C L O S E D",
                "APPOINTMENT REQUIREMENT");

private static List<String> getSortedDates() {
    EnumSet<DayOfWeek> dayOfWeeks = EnumSet.allOf(DayOfWeek.class);
    return DATES.stream()
            .map(p -> dayOfWeeks.stream()
                    .filter(q -> p.startsWith(q.name()))
                    .findAny()
                    .map(r -> new SimpleImmutableEntry<>(r.ordinal(), p))
                    .orElse(new SimpleImmutableEntry<>(dayOfWeeks.size(), p)))
            .sorted(Comparator.comparing(SimpleImmutableEntry::getKey))
            .map(SimpleImmutableEntry::getValue)
            .collect(Collectors.toList());
}

public static void main(String[] args) {
    getSortedDates().forEach(System.out::println);
}