I am looking to sort the strings based on
- Date(yyMMdd) present in string.
- Dynamic substring of the given string.(att, dscl, xml)
These are the input strings.
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
Expected result: Sorted based on date (220520, 220523, 220524) and all the same date files should be sorted in the order where .att file first, .dscl file second and rest all .xml files 3rd position on wards.
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
Tried extracting date and sorting based on date but i am not getting how to achieve it when date and dynamic substring is part of strings which i want to sort.
Could you please help?
Code tried.
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);
}
}
output:
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]
But the expected list:
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]
It is sorting based on date but not getting how to sort them with 2nd condition i.e in the order of att, dscl and xml for within same date.