我希望根据
- 日期(yyMMdd)以字符串形式显示.
- 给定字符串的动态子字符串.(附件、dscl、xml)
这些是输入字符串.
f1660.msg.220523.xml.gpg
f1660.msg.dscl.220523.xml.gpg
f1660.msg.att.220523.tar.gz.gpg
f1660.cit.msg.220520.xml1.gpg
f1660.cit.msg.220520.xml2.gpg
f1660.cit.msg.att.220520.tar.gz.gpg
f1660.cit.msg.dscl.220520.xml.gpg
8701D6.msg.220524.xml.gpg
8701D6.dscl.220524.xml.gpg
8701D6.msg.att.220524.tar.gz.gpg
预期结果:根据日期(220520、220523、220524)排序,所有相同日期的文件应按其中的顺序排序.att文件优先.dscl文件第二个,其余全部.xml文件位于病房的第三位.
f1660.cit.msg.att.220520.tar.gz.gpg
f1660.cit.msg.dscl.220520.xml.gpg
f1660.cit.msg.220520.xml1.gpg
f1660.cit.msg.220520.xml2.gpg
f1660.msg.att.220523.tar.gz.gpg
f1660.msg.dscl.220523.xml.gpg
f1660.msg.220523.xml.gpg
8701D6.msg.att.220524.tar.gz.gpg
8701D6.dscl.220524.xml.gpg
8701D6.msg.220524.xml.gpg
try 提取日期并基于日期排序,但当日期和动态子字符串是我要排序的字符串的一部分时,我不知道如何实现它.
你能帮忙吗?
已try 代码.
public void sortBasedOnDate() {
ArrayList<String> names = new ArrayList<>();
names.add("f1660.msg.220523.xml.gpg");
names.add("f1660.msg.dscl.220523.xml.gpg");
names.add("f1660.msg.att.220523.tar.gz.gpg");
names.add("f1660.cit.msg.220520.xml1.gpg");
names.add("f1660.cit.msg.220520.xml2.gpg");
names.add("f1660.cit.msg.att.220520.tar.gz.gpg");
names.add("f1660.cit.msg.dscl.220520.xml.gpg");
names.add("8701D6.msg.220524.xml.gpg");
names.add("8701D6.dscl.220524.xml.gpg");
names.add("8701D6.msg.att.220524.tar.gz.gpg");
TreeMap<LocalDate, List<String>> map = new TreeMap<>(new SortByDate());
for (String name : names) {
LocalDate localDate = getDateFromFileName(name);
List<String> mapList = map.get(localDate);
if (mapList == null) {
mapList = new ArrayList<>();
mapList.add(name);
map.put(localDate, mapList);
} else {
mapList.add(name);
}
}
List<String> sortedList = new LinkedList<>();
for (Map.Entry<LocalDate, List<String>> entry : map.entrySet()) {
sortedList.addAll(entry.getValue());
}
System.out.println("sortedList: "+sortedList);
}
private LocalDate getDateFromFileName(String fileName) {
String fileUtilDateStr = "";
Optional<String> streamResult = Arrays.stream(fileName.split("\\."))
.filter(element -> Pattern.compile("(\\d){6}").matcher(element).matches()).findFirst();
if (streamResult.isPresent()) {
fileUtilDateStr = streamResult.get();
}
DateTimeFormatter formatters = DateTimeFormatter.ofPattern("yyMMdd");
LocalDate parsedDate = LocalDate.parse(fileUtilDateStr, formatters);
return parsedDate;
}
public class SortByDate implements Comparator<LocalDate> {
@Override
public int compare(LocalDate date1, LocalDate date2) {
return date1.compareTo(date2);
}
}
输出:
sortedList: [f1660.cit.msg.220520.xml1.gpg,
f1660.cit.msg.220520.xml2.gpg, f1660.cit.msg.att.220520.tar.gz.gpg, f1660.cit.msg.dscl.220520.xml.gpg, f1660.msg.220523.xml.gpg, f1660.msg.dscl.220523.xml.gpg, f1660.msg.att.220523.tar.gz.gpg, 8701D6.msg.220524.xml.gpg, 8701D6.dscl.220524.xml.gpg, 8701D6.msg.att.220524.tar.gz.gpg]
但预期列表:
sortedList: [f1660.cit.msg.att.220520.tar.gz.gpg,
f1660.cit.msg.dscl.220520.xml.gpg, f1660.cit.msg.220520.xml1.gpg, f1660.cit.msg.220520.xml2.gpg, f1660.msg.att.220523.tar.gz.gpg, f1660.msg.dscl.220523.xml.gpg, f1660.msg.220523.xml.gpg, 8701D6.msg.att.220524.tar.gz.gpg, 8701D6.dscl.220524.xml.gpg, 8701D6.msg.220524.xml.gpg]
它是基于日期进行排序,但不知道如何使用第二个条件进行排序,即在同一日期内按照att、dscl和xml的顺序进行排序.